llvieweraudio.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:11k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file llvieweraudio.cpp
  3.  * @brief Audio functions that used to be in viewer.cpp
  4.  *
  5.  * $LicenseInfo:firstyear=2002&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2002-2010, 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 "llaudioengine.h"
  34. #include "llagent.h"
  35. #include "llappviewer.h"
  36. #include "llvieweraudio.h"
  37. #include "llviewercamera.h"
  38. #include "llviewercontrol.h"
  39. #include "llviewerwindow.h"
  40. #include "llvoiceclient.h"
  41. #include "llviewermedia.h"
  42. /////////////////////////////////////////////////////////
  43. void init_audio() 
  44. {
  45. if (!gAudiop) 
  46. {
  47. llwarns << "Failed to create an appropriate Audio Engine" << llendl;
  48. return;
  49. }
  50. LLVector3d lpos_global = gAgent.getCameraPositionGlobal();
  51. LLVector3 lpos_global_f;
  52. lpos_global_f.setVec(lpos_global);
  53. gAudiop->setListener(lpos_global_f,
  54.   LLVector3::zero, // LLViewerCamera::getInstance()->getVelocity(),    // !!! BUG need to replace this with smoothed velocity!
  55.   LLViewerCamera::getInstance()->getUpAxis(),
  56.   LLViewerCamera::getInstance()->getAtAxis());
  57. // load up our initial set of sounds we'll want so they're in memory and ready to be played
  58. BOOL mute_audio = gSavedSettings.getBOOL("MuteAudio");
  59. if (!mute_audio && FALSE == gSavedSettings.getBOOL("NoPreload"))
  60. {
  61. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndAlert")));
  62. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndBadKeystroke")));
  63. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndChatFromObject")));
  64. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndClick")));
  65. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndClickRelease")));
  66. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndHealthReductionF")));
  67. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndHealthReductionM")));
  68. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndIncomingChat")));
  69. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndIncomingIM")));
  70. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndInvApplyToObject")));
  71. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndInvalidOp")));
  72. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndInventoryCopyToInv")));
  73. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndMoneyChangeDown")));
  74. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndMoneyChangeUp")));
  75. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndObjectCopyToInv")));
  76. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndObjectCreate")));
  77. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndObjectDelete")));
  78. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndObjectRezIn")));
  79. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndObjectRezOut")));
  80. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndSnapshot")));
  81. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndStartAutopilot")));
  82. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndStartFollowpilot")));
  83. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndStartIM")));
  84. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndStopAutopilot")));
  85. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndTeleportOut")));
  86. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndTextureApplyToObject")));
  87. //gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndTextureCopyToInv")));
  88. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndTyping")));
  89. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndWindowClose")));
  90. gAudiop->preloadSound(LLUUID(gSavedSettings.getString("UISndWindowOpen")));
  91. }
  92. audio_update_volume(true);
  93. }
  94. void audio_update_volume(bool force_update)
  95. {
  96. F32 master_volume = gSavedSettings.getF32("AudioLevelMaster");
  97. BOOL mute_audio = gSavedSettings.getBOOL("MuteAudio");
  98. if (!gViewerWindow->getActive() && (gSavedSettings.getBOOL("MuteWhenMinimized")))
  99. {
  100. mute_audio = TRUE;
  101. }
  102. F32 mute_volume = mute_audio ? 0.0f : 1.0f;
  103. // Sound Effects
  104. if (gAudiop) 
  105. {
  106. gAudiop->setMasterGain ( master_volume );
  107. gAudiop->setDopplerFactor(gSavedSettings.getF32("AudioLevelDoppler"));
  108. gAudiop->setRolloffFactor(gSavedSettings.getF32("AudioLevelRolloff"));
  109. #ifdef kAUDIO_ENABLE_WIND
  110. gAudiop->enableWind(!mute_audio);
  111. #endif
  112. gAudiop->setMuted(mute_audio);
  113. if (force_update)
  114. {
  115. audio_update_wind(true);
  116. }
  117. // handle secondary gains
  118. gAudiop->setSecondaryGain(LLAudioEngine::AUDIO_TYPE_SFX,
  119.   gSavedSettings.getBOOL("MuteSounds") ? 0.f : gSavedSettings.getF32("AudioLevelSFX"));
  120. gAudiop->setSecondaryGain(LLAudioEngine::AUDIO_TYPE_UI,
  121.   gSavedSettings.getBOOL("MuteUI") ? 0.f : gSavedSettings.getF32("AudioLevelUI"));
  122. gAudiop->setSecondaryGain(LLAudioEngine::AUDIO_TYPE_AMBIENT,
  123.   gSavedSettings.getBOOL("MuteAmbient") ? 0.f : gSavedSettings.getF32("AudioLevelAmbient"));
  124. }
  125. // Streaming Music
  126. if (gAudiop) 
  127. {
  128. F32 music_volume = gSavedSettings.getF32("AudioLevelMusic");
  129. BOOL music_muted = gSavedSettings.getBOOL("MuteMusic");
  130. music_volume = mute_volume * master_volume * (music_volume*music_volume);
  131. gAudiop->setInternetStreamGain ( music_muted ? 0.f : music_volume );
  132. }
  133. // Streaming Media
  134. F32 media_volume = gSavedSettings.getF32("AudioLevelMedia");
  135. BOOL media_muted = gSavedSettings.getBOOL("MuteMedia");
  136. media_volume = mute_volume * master_volume * (media_volume*media_volume);
  137. LLViewerMedia::setVolume( media_muted ? 0.0f : media_volume );
  138. // Voice
  139. if (gVoiceClient)
  140. {
  141. F32 voice_volume = gSavedSettings.getF32("AudioLevelVoice");
  142. voice_volume = mute_volume * master_volume * voice_volume;
  143. BOOL voice_mute = gSavedSettings.getBOOL("MuteVoice");
  144. gVoiceClient->setVoiceVolume(voice_mute ? 0.f : voice_volume);
  145. gVoiceClient->setMicGain(voice_mute ? 0.f : gSavedSettings.getF32("AudioLevelMic"));
  146. if (!gViewerWindow->getActive() && (gSavedSettings.getBOOL("MuteWhenMinimized")))
  147. {
  148. gVoiceClient->setMuteMic(true);
  149. }
  150. else
  151. {
  152. gVoiceClient->setMuteMic(false);
  153. }
  154. }
  155. }
  156. void audio_update_listener()
  157. {
  158. if (gAudiop)
  159. {
  160. // update listener position because agent has moved
  161. LLVector3d lpos_global = gAgent.getCameraPositionGlobal();
  162. LLVector3 lpos_global_f;
  163. lpos_global_f.setVec(lpos_global);
  164. gAudiop->setListener(lpos_global_f,
  165.  // LLViewerCamera::getInstance()VelocitySmoothed, 
  166.  // LLVector3::zero,
  167.  gAgent.getVelocity(),    // !!! *TODO: need to replace this with smoothed velocity!
  168.  LLViewerCamera::getInstance()->getUpAxis(),
  169.  LLViewerCamera::getInstance()->getAtAxis());
  170. }
  171. }
  172. void audio_update_wind(bool force_update)
  173. {
  174. #ifdef kAUDIO_ENABLE_WIND
  175. //
  176. //  Extract height above water to modulate filter by whether above/below water 
  177. // 
  178. LLViewerRegion* region = gAgent.getRegion();
  179. if (region)
  180. {
  181. static F32 last_camera_water_height = -1000.f;
  182. LLVector3 camera_pos = gAgent.getCameraPositionAgent();
  183. F32 camera_water_height = camera_pos.mV[VZ] - region->getWaterHeight();
  184. //
  185. //  Don't update rolloff factor unless water surface has been crossed
  186. //
  187. if (force_update || (last_camera_water_height * camera_water_height) < 0.f)
  188. {
  189.             static LLUICachedControl<F32> rolloff("AudioLevelRolloff", 1.0f);
  190. if (camera_water_height < 0.f)
  191. {
  192. gAudiop->setRolloffFactor(rolloff * LL_ROLLOFF_MULTIPLIER_UNDER_WATER);
  193. }
  194. else 
  195. {
  196. gAudiop->setRolloffFactor(rolloff);
  197. }
  198. }
  199.         
  200.         // Scale down the contribution of weather-simulation wind to the
  201.         // ambient wind noise.  Wind velocity averages 3.5 m/s, with gusts to 7 m/s
  202.         // whereas steady-state avatar walk velocity is only 3.2 m/s.
  203.         // Without this the world feels desolate on first login when you are
  204.         // standing still.
  205.         static LLUICachedControl<F32> wind_level("AudioLevelWind", 0.5f);
  206.         LLVector3 scaled_wind_vec = gWindVec * wind_level;
  207.         
  208.         // Mix in the avatar's motion, subtract because when you walk north,
  209.         // the apparent wind moves south.
  210.         LLVector3 final_wind_vec = scaled_wind_vec - gAgent.getVelocity();
  211.         
  212. // rotate the wind vector to be listener (agent) relative
  213. gRelativeWindVec = gAgent.getFrameAgent().rotateToLocal( final_wind_vec );
  214. // don't use the setter setMaxWindGain() because we don't
  215. // want to screw up the fade-in on startup by setting actual source gain
  216. // outside the fade-in.
  217. F32 master_volume  = gSavedSettings.getBOOL("MuteAudio") ? 0.f : gSavedSettings.getF32("AudioLevelMaster");
  218. F32 ambient_volume = gSavedSettings.getBOOL("MuteAmbient") ? 0.f : gSavedSettings.getF32("AudioLevelAmbient");
  219. F32 max_wind_volume = master_volume * ambient_volume;
  220. const F32 WIND_SOUND_TRANSITION_TIME = 2.f;
  221. // amount to change volume this frame
  222. F32 volume_delta = (LLFrameTimer::getFrameDeltaTimeF32() / WIND_SOUND_TRANSITION_TIME) * max_wind_volume;
  223. if (force_update) 
  224. {
  225. // initialize wind volume (force_update) by using large volume_delta
  226. // which is sufficient to completely turn off or turn on wind noise
  227. volume_delta = 1.f;
  228. }
  229. // mute wind when not flying
  230. if (gAgent.getFlying())
  231. {
  232. // volume increases by volume_delta, up to no more than max_wind_volume
  233. gAudiop->mMaxWindGain = llmin(gAudiop->mMaxWindGain + volume_delta, max_wind_volume);
  234. }
  235. else
  236. {
  237. // volume decreases by volume_delta, down to no less than 0
  238. gAudiop->mMaxWindGain = llmax(gAudiop->mMaxWindGain - volume_delta, 0.f);
  239. }
  240. last_camera_water_height = camera_water_height;
  241. gAudiop->updateWind(gRelativeWindVec, camera_water_height);
  242. }
  243. #endif
  244. }