llvoicevisualizer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**
  2. * @file llvoicevisualizer.cpp
  3. * @brief Draws in-world speaking indicators.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, JJ Ventrella & 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. #include "llviewerprecompiledheaders.h"
  33. #include "llvoicevisualizer.h"
  34. #include "llgl.h"
  35. #include "llimagegl.h"
  36. #include "llrender.h"
  37. #include "llviewercamera.h"
  38. #include "llviewercontrol.h"
  39. #include "llviewerobject.h"
  40. #include "llviewertexturelist.h"
  41. #include "llvoiceclient.h"
  42. // Brent's wave image: 29de489d-0491-fb00-7dab-f9e686d31e83
  43. // Sound symbol constants
  44. // How many meters vertically above the av's head the voice symbol will appear
  45. constexpr F32 HEIGHT_ABOVE_HEAD = 0.3f;
  46. // Value above which speaking amplitude causes the voice symbol to turn red
  47. constexpr F32 RED_THRESHOLD = OVERDRIVEN_POWER_LEVEL;
  48. // Value above which speaking amplitude causes the voice symbol to turn green
  49. constexpr F32 GREEN_THRESHOLD = 0.2f;
  50. // How many seconds it takes for a pair of waves to fade away
  51. constexpr F32 FADE_OUT_DURATION = 0.4f;
  52. // How many seconds it takes for the waves to expand to twice their orig. size
  53. constexpr F32 EXPANSION_RATE = 1.f;
  54. // Maximum size scale to which the waves can expand before popping back to 1.0
  55. constexpr F32 EXPANSION_MAX = 1.5f;
  56. constexpr F32 WAVE_WIDTH_SCALE = 0.03f; // Base width of the waves
  57. constexpr F32 WAVE_HEIGHT_SCALE = 0.02f; // Base height of the waves
  58. // Gray level of the voice indicator when quiet (below green threshold)
  59. constexpr F32 BASE_BRIGHTNESS = 0.7f;
  60. constexpr F32 DOT_SIZE = 0.05f; // Size of the dot billboard texture
  61. constexpr F32 DOT_OPACITY = 0.7f; // How opaque the dot is
  62. // Scalar applied to consecutive waves as a function of speaking amplitude
  63. constexpr F32 WAVE_MOTION_RATE = 1.5f;
  64. // Gesticulation constants
  65. constexpr F32 DEFAULT_MINIMUM_GESTICULATION_AMPLITUDE = 0.2f;
  66. constexpr F32 DEFAULT_MAXIMUM_GESTICULATION_AMPLITUDE = 1.f;
  67. // Other constants
  68. // Handles parameter updates
  69. static bool handleVoiceVisualizerPrefsChanged(const LLSD& newvalue)
  70. {
  71. // Note: Ignore the specific event value, we look up the ones we want
  72. LLVoiceVisualizer::setPreferences();
  73. return true;
  74. }
  75. // Statics
  76. bool LLVoiceVisualizer::sPrefsInitialized = false;
  77. bool LLVoiceVisualizer::sLipSyncEnabled = false;
  78. F32* LLVoiceVisualizer::sOoh = NULL;
  79. F32* LLVoiceVisualizer::sAah = NULL;
  80. U32 LLVoiceVisualizer::sOohs = 0;
  81. U32 LLVoiceVisualizer::sAahs = 0;
  82. F32 LLVoiceVisualizer::sOohAahRate = 0.f;
  83. F32* LLVoiceVisualizer::sOohPowerTransfer = NULL;
  84. U32 LLVoiceVisualizer::sOohPowerTransfers = 0;
  85. F32 LLVoiceVisualizer::sOohPowerTransfersf = 0.f;
  86. F32* LLVoiceVisualizer::sAahPowerTransfer = NULL;
  87. U32 LLVoiceVisualizer::sAahPowerTransfers = 0;
  88. F32 LLVoiceVisualizer::sAahPowerTransfersf = 0.f;
  89. LLVoiceVisualizer::LLVoiceVisualizer(U8 type)
  90. : LLHUDEffect(type),
  91. mVoiceEnabled(false),
  92. mSpeakingAmplitude(0.f),
  93. mCurrentlySpeaking(false),
  94. mMinGesticulationAmplitude(DEFAULT_MINIMUM_GESTICULATION_AMPLITUDE),
  95. mMaxGesticulationAmplitude(DEFAULT_MAXIMUM_GESTICULATION_AMPLITUDE)
  96. {
  97. mStartTime = mCurrentTime = mPreviousTime = mTimer.getTotalSeconds();
  98. mTimer.reset();
  99. static const char* sound_level_img[] =
  100. {
  101. "041ee5a0-cb6a-9ac5-6e49-41e9320507d5.j2c",
  102. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c",
  103. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c",
  104. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c",
  105. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c",
  106. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c",
  107. "29de489d-0491-fb00-7dab-f9e686d31e83.j2c"
  108. };
  109. mSoundSymbol.mActive = true;
  110. for (U32 i = 0; i < NUM_VOICE_WAVES; ++i)
  111. {
  112. mSoundSymbol.mWaveFadeOutStartTime[i] = mCurrentTime;
  113. mSoundSymbol.mTexture[i] =
  114. LLViewerTextureManager::getFetchedTextureFromFile(sound_level_img[i],
  115. MIPMAP_NO);
  116. mSoundSymbol.mWaveActive[i] = false;
  117. mSoundSymbol.mWaveOpacity[i] = mSoundSymbol.mWaveExpansion[i] = 1.f;
  118. }
  119. mSoundSymbol.mTexture[0]->setFilteringOption(LLTexUnit::TFO_ANISOTROPIC);
  120. // The first instance loads the initial state from prefs.
  121. if (sPrefsInitialized)
  122. {
  123. return;
  124. }
  125. sPrefsInitialized = true;
  126. setPreferences();
  127. // Set up our listener to get updates on all prefs values we care about.
  128. gSavedSettings.getControl("LipSyncEnabled")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  129. gSavedSettings.getControl("LipSyncOohAahRate")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  130. gSavedSettings.getControl("LipSyncOoh")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  131. gSavedSettings.getControl("LipSyncAah")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  132. gSavedSettings.getControl("LipSyncOohPowerTransfer")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  133. gSavedSettings.getControl("LipSyncAahPowerTransfer")->getSignal()->connect(boost::bind(&handleVoiceVisualizerPrefsChanged, _2));
  134. }
  135. void LLVoiceVisualizer::setStartSpeaking()
  136. {
  137. mStartTime = mTimer.getTotalSeconds();
  138. mCurrentlySpeaking = mSoundSymbol.mActive = true;
  139. }
  140. void LLVoiceVisualizer::setStopSpeaking()
  141. {
  142. mCurrentlySpeaking = false;
  143. mSpeakingAmplitude = 0.f;
  144. }
  145. void LLVoiceVisualizer::setPreferences()
  146. {
  147. sLipSyncEnabled = gSavedSettings.getBool("LipSyncEnabled");
  148. sOohAahRate = gSavedSettings.getF32("LipSyncOohAahRate");
  149. std::string str = gSavedSettings.getString("LipSyncOoh");
  150. lipStringToF32s(str, sOoh, sOohs);
  151. str = gSavedSettings.getString("LipSyncAah");
  152. lipStringToF32s(str, sAah, sAahs);
  153. str = gSavedSettings.getString("LipSyncOohPowerTransfer");
  154. lipStringToF32s(str, sOohPowerTransfer, sOohPowerTransfers);
  155. sOohPowerTransfersf = (F32)sOohPowerTransfers;
  156. str = gSavedSettings.getString("LipSyncAahPowerTransfer");
  157. lipStringToF32s(str, sAahPowerTransfer, sAahPowerTransfers);
  158. sAahPowerTransfersf = (F32)sAahPowerTransfers;
  159. }
  160. // Converts a string of digits to an array of floats. The result for each digit
  161. // is the value of the digit multiplied by 0.11
  162. void LLVoiceVisualizer::lipStringToF32s(std::string& in_string,
  163. F32*& out, U32& count)
  164. {
  165. delete[] out; // Get rid of the current array
  166. count = in_string.length();
  167. if (!count)
  168. {
  169. // We do not like zero length arrays
  170. count = 1;
  171. out = new F32[1];
  172. out[0] = 0.f;
  173. return;
  174. }
  175. out = new F32[count];
  176. for (U32 i = 0; i < count; ++i)
  177. {
  178. // We convert the characters 0 to 9 to their numeric value. Anything
  179. // else we take the low order four bits with a ceiling of 9
  180. U8 digit = in_string[i];
  181. U8 four_bits = digit % 16;
  182. if (four_bits > 9)
  183. {
  184. four_bits = 9;
  185. }
  186. out[i] = 0.11f * (F32)four_bits;
  187. }
  188. }
  189. // Finds the amount to blend the ooh and aah mouth morphs
  190. void LLVoiceVisualizer::lipSyncOohAah(F32& ooh, F32& aah)
  191. {
  192. if (sLipSyncEnabled && mCurrentlySpeaking)
  193. {
  194. U32 transfer_index = U32(sOohPowerTransfersf * mSpeakingAmplitude);
  195. if (transfer_index >= sOohPowerTransfers)
  196. {
  197. transfer_index = sOohPowerTransfers - 1;
  198. }
  199. F32 transfer_ooh = sOohPowerTransfer[transfer_index];
  200. transfer_index = U32(sAahPowerTransfersf * mSpeakingAmplitude);
  201. if (transfer_index >= sAahPowerTransfers)
  202. {
  203. transfer_index = sAahPowerTransfers - 1;
  204. }
  205. F32 transfer_aah = sAahPowerTransfer[transfer_index];
  206. F64 current_time = mTimer.getTotalSeconds();
  207. F64 elapsed_time = current_time - mStartTime;
  208. U32 elapsed_frames = U32(elapsed_time * sOohAahRate);
  209. U32 elapsed_oohs = elapsed_frames % sOohs;
  210. U32 elapsed_aahs = elapsed_frames % sAahs;
  211. ooh = transfer_ooh * sOoh[elapsed_oohs];
  212. aah = transfer_aah * sAah[elapsed_aahs];
  213. }
  214. else
  215. {
  216. ooh = aah = 0.f;
  217. }
  218. }
  219. //virtual
  220. void LLVoiceVisualizer::render()
  221. {
  222. if (!mVoiceEnabled || !mSoundSymbol.mActive)
  223. {
  224. return;
  225. }
  226. mPreviousTime = mCurrentTime;
  227. mCurrentTime = mTimer.getTotalSeconds();
  228. // Set the sound symbol position over the source (avatar's head)
  229. mSoundSymbol.mPosition = mVoiceSourceWorldPosition +
  230. LLVector3::z_axis * HEIGHT_ABOVE_HEAD;
  231. // Some GL states
  232. LLGLSPipelineAlpha alpha_blend;
  233. LLGLDepthTest depth(GL_TRUE, GL_FALSE);
  234. // Create coordinates of the geometry for the dot
  235. LLVector3 l = gViewerCamera.getLeftAxis() * DOT_SIZE;
  236. LLVector3 u = gViewerCamera.getUpAxis() * DOT_SIZE;
  237. LLVector3 bottom_left = mSoundSymbol.mPosition + l - u;
  238. LLVector3 bottom_right = mSoundSymbol.mPosition - l - u;
  239. LLVector3 top_left = mSoundSymbol.mPosition + l + u;
  240. LLVector3 top_right = mSoundSymbol.mPosition - l + u;
  241. // Bind texture 0 (the dot)
  242. LLTexUnit* unit0 = gGL.getTexUnit(0);
  243. unit0->bind(mSoundSymbol.mTexture[0]);
  244. // Now render the dot
  245. gGL.color4fv(LLColor4(1.f, 1.f, 1.f, DOT_OPACITY).mV);
  246. gGL.begin(LLRender::TRIANGLE_STRIP);
  247. gGL.texCoord2i(0, 0);
  248. gGL.vertex3fv(bottom_left.mV);
  249. gGL.texCoord2i(1, 0);
  250. gGL.vertex3fv(bottom_right.mV);
  251. gGL.texCoord2i(0, 1);
  252. gGL.vertex3fv(top_left.mV);
  253. gGL.end();
  254. gGL.begin(LLRender::TRIANGLE_STRIP);
  255. gGL.texCoord2i(1, 0);
  256. gGL.vertex3fv(bottom_right.mV);
  257. gGL.texCoord2i(1, 1);
  258. gGL.vertex3fv(top_right.mV);
  259. gGL.texCoord2i(0, 1);
  260. gGL.vertex3fv(top_left.mV);
  261. gGL.end();
  262. // If currently speaking, trigger waves (1 through 6) based on speaking
  263. // amplitude
  264. if (mCurrentlySpeaking)
  265. {
  266. F32 min = 0.2f;
  267. F32 max = 0.7f;
  268. F32 fraction = (mSpeakingAmplitude - min) / (max - min);
  269. // In case mSpeakingAmplitude > max....
  270. if (fraction > 1.f)
  271. {
  272. fraction = 1.f;
  273. }
  274. S32 level = 1 + (S32)(fraction * (NUM_VOICE_WAVES - 2));
  275. for (S32 i = 0; i < level + 1; ++i)
  276. {
  277. mSoundSymbol.mWaveActive[i] = true;
  278. mSoundSymbol.mWaveOpacity[i] = 1.f;
  279. mSoundSymbol.mWaveFadeOutStartTime[i] = mCurrentTime;
  280. }
  281. }
  282. // Determine color
  283. F32 red = 0.f;
  284. F32 green = 0.f;
  285. F32 blue = 0.f;
  286. if (mSpeakingAmplitude < RED_THRESHOLD)
  287. {
  288. if (mSpeakingAmplitude < GREEN_THRESHOLD)
  289. {
  290. red = green = blue = BASE_BRIGHTNESS;
  291. }
  292. else
  293. {
  294. // Fade from gray to bright green
  295. F32 fraction = (mSpeakingAmplitude - GREEN_THRESHOLD) /
  296. (1.f - GREEN_THRESHOLD);
  297. red = BASE_BRIGHTNESS - fraction * BASE_BRIGHTNESS;
  298. green = BASE_BRIGHTNESS + fraction * (1.f - BASE_BRIGHTNESS);
  299. blue = BASE_BRIGHTNESS - fraction * BASE_BRIGHTNESS;
  300. }
  301. }
  302. else
  303. {
  304. // Redish
  305. red = 1.f;
  306. green = blue = 0.2f;
  307. }
  308. for (U32 i = 0; i < NUM_VOICE_WAVES; ++i)
  309. {
  310. if (!mSoundSymbol.mWaveActive[i])
  311. {
  312. continue;
  313. }
  314. F32 fade_out_frac = (F32)(mCurrentTime -
  315. mSoundSymbol.mWaveFadeOutStartTime[i]) /
  316. FADE_OUT_DURATION;
  317. mSoundSymbol.mWaveOpacity[i] = 1.f - fade_out_frac;
  318. if (mSoundSymbol.mWaveOpacity[i] < 0.f)
  319. {
  320. mSoundSymbol.mWaveFadeOutStartTime[i] = mCurrentTime;
  321. mSoundSymbol.mWaveOpacity[i] = 0.f;
  322. mSoundSymbol.mWaveActive[i] = false;
  323. }
  324. // This is where we calculate the expansion of the waves, that is, the
  325. // rate at which they are scaled greater than 1.0 so that they grow
  326. // over time.
  327. F32 time_slice = F32(mCurrentTime - mPreviousTime);
  328. F32 wave_speed = mSpeakingAmplitude * WAVE_MOTION_RATE;
  329. mSoundSymbol.mWaveExpansion[i] *= 1.f + EXPANSION_RATE * time_slice *
  330. wave_speed;
  331. if (mSoundSymbol.mWaveExpansion[i] > EXPANSION_MAX)
  332. {
  333. mSoundSymbol.mWaveExpansion[i] = 1.f;
  334. }
  335. // Create geometry for the wave billboard textures
  336. F32 width = i * WAVE_WIDTH_SCALE * mSoundSymbol.mWaveExpansion[i];
  337. F32 height = i * WAVE_HEIGHT_SCALE * mSoundSymbol.mWaveExpansion[i];
  338. LLVector3 l = gViewerCamera.getLeftAxis() * width;
  339. LLVector3 u = gViewerCamera.getUpAxis() * height;
  340. LLVector3 bottom_left = mSoundSymbol.mPosition + l - u;
  341. LLVector3 bottom_right = mSoundSymbol.mPosition - l - u;
  342. LLVector3 top_left = mSoundSymbol.mPosition + l + u;
  343. LLVector3 top_right = mSoundSymbol.mPosition - l + u;
  344. gGL.color4fv(LLColor4(red, green, blue,
  345. mSoundSymbol.mWaveOpacity[i]).mV);
  346. unit0->bind(mSoundSymbol.mTexture[i]);
  347. // Now, render the mofo
  348. gGL.begin(LLRender::TRIANGLE_STRIP);
  349. gGL.texCoord2i(0, 0);
  350. gGL.vertex3fv(bottom_left.mV);
  351. gGL.texCoord2i(1, 0);
  352. gGL.vertex3fv(bottom_right.mV);
  353. gGL.texCoord2i(0, 1);
  354. gGL.vertex3fv(top_left.mV);
  355. gGL.end();
  356. gGL.begin(LLRender::TRIANGLE_STRIP);
  357. gGL.texCoord2i(1, 0);
  358. gGL.vertex3fv(bottom_right.mV);
  359. gGL.texCoord2i(1, 1);
  360. gGL.vertex3fv(top_right.mV);
  361. gGL.texCoord2i(0, 1);
  362. gGL.vertex3fv(top_left.mV);
  363. gGL.end();
  364. }
  365. }
  366. VoiceGesticulationLevel LLVoiceVisualizer::getCurrentGesticulationLevel()
  367. {
  368. VoiceGesticulationLevel level = VOICE_GESTICULATION_LEVEL_OFF;
  369. // Within the range of gesticulation amplitudes, the sound signal is split
  370. // into three equal amplitude regimes, each specifying one of three
  371. // gesticulation levels.
  372. F32 range = mMaxGesticulationAmplitude - mMinGesticulationAmplitude;
  373. if (mSpeakingAmplitude > mMinGesticulationAmplitude + range * 0.5f)
  374. {
  375. level = VOICE_GESTICULATION_LEVEL_HIGH;
  376. }
  377. else if (mSpeakingAmplitude > mMinGesticulationAmplitude + range * 0.25f)
  378. {
  379. level = VOICE_GESTICULATION_LEVEL_MEDIUM;
  380. }
  381. else if (mSpeakingAmplitude > mMinGesticulationAmplitude)
  382. {
  383. level = VOICE_GESTICULATION_LEVEL_LOW;
  384. }
  385. return level;
  386. }
  387. //virtual
  388. void LLVoiceVisualizer::packData(LLMessageSystem *mesgsys)
  389. {
  390. // Pack the default data
  391. LLHUDEffect::packData(mesgsys);
  392. #if 0 // *TODO: pack the relevant data for voice effects. We will come up
  393. // with some cool configurations... TBD
  394. U8 packed_data[41];
  395. mesgsys->addBinaryDataFast(_PREHASH_TypeData, packed_data, 41);
  396. #endif
  397. U8 packed_data = 0;
  398. mesgsys->addBinaryDataFast(_PREHASH_TypeData, &packed_data, 1);
  399. }
  400. //virtual
  401. void LLVoiceVisualizer::unpackData(LLMessageSystem *mesgsys, S32 blocknum)
  402. {
  403. #if 0 // *TODO: find the speaker, unpack binary data, set the properties of
  404. // this effect
  405. LLHUDEffect::unpackData(mesgsys, blocknum);
  406. S32 size = mesgsys->getSizeFast(_PREHASH_Effect, blocknum,
  407. _PREHASH_TypeData);
  408. if (size != 1)
  409. {
  410. llwarns << "Voice effect with bad size " << size << llendl;
  411. return;
  412. }
  413. mesgsys->getBinaryDataFast(_PREHASH_Effect, _PREHASH_TypeData, packed_data,
  414. 1, blocknum);
  415. #endif
  416. }
  417. //virtual
  418. void LLVoiceVisualizer::markDead()
  419. {
  420. mCurrentlySpeaking = mVoiceEnabled = mSoundSymbol.mActive = false;
  421. LLHUDEffect::markDead();
  422. }