SoundModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using log4net;
  34. using Mono.Addins;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. namespace OpenSim.Region.CoreModules.World.Sound
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SoundModule")]
  41. public class SoundModule : INonSharedRegionModule, ISoundModule
  42. {
  43. // private static readonly ILog m_log = LogManager.GetLogger(
  44. // MethodBase.GetCurrentMethod().DeclaringType);
  45. private Scene m_scene;
  46. public bool Enabled { get; private set; }
  47. public float MaxDistance { get; private set; }
  48. #region INonSharedRegionModule
  49. public void Initialise(IConfigSource configSource)
  50. {
  51. IConfig config = configSource.Configs["Sounds"];
  52. if (config == null)
  53. {
  54. Enabled = true;
  55. MaxDistance = 100.0f;
  56. }
  57. else
  58. {
  59. Enabled = config.GetString("Module", "OpenSim.Region.CoreModules.dll:SoundModule") ==
  60. Path.GetFileName(Assembly.GetExecutingAssembly().Location)
  61. + ":" + MethodBase.GetCurrentMethod().DeclaringType.Name;
  62. MaxDistance = config.GetFloat("MaxDistance", 100.0f);
  63. }
  64. }
  65. public void AddRegion(Scene scene) { }
  66. public void RemoveRegion(Scene scene)
  67. {
  68. m_scene.EventManager.OnNewClient -= OnNewClient;
  69. }
  70. public void RegionLoaded(Scene scene)
  71. {
  72. if (!Enabled)
  73. return;
  74. m_scene = scene;
  75. m_scene.EventManager.OnNewClient += OnNewClient;
  76. m_scene.RegisterModuleInterface<ISoundModule>(this);
  77. }
  78. public void Close() { }
  79. public Type ReplaceableInterface
  80. {
  81. get { return typeof(ISoundModule); }
  82. }
  83. public string Name { get { return "Sound Module"; } }
  84. #endregion
  85. #region Event Handlers
  86. private void OnNewClient(IClientAPI client)
  87. {
  88. client.OnSoundTrigger += TriggerSound;
  89. }
  90. #endregion
  91. #region ISoundModule
  92. public virtual void PlayAttachedSound(
  93. UUID soundID, UUID ownerID, UUID objectID, double gain, Vector3 position, byte flags, float radius)
  94. {
  95. SceneObjectPart part;
  96. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  97. return;
  98. SceneObjectGroup grp = part.ParentGroup;
  99. if (radius == 0)
  100. radius = MaxDistance;
  101. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  102. {
  103. double dis = Util.GetDistanceTo(sp.AbsolutePosition, position);
  104. if (dis > MaxDistance) // Max audio distance
  105. return;
  106. if (grp.IsAttachment)
  107. {
  108. if (grp.HasPrivateAttachmentPoint && sp.ControllingClient.AgentId != grp.OwnerID)
  109. return;
  110. if (sp.ControllingClient.AgentId == grp.OwnerID)
  111. dis = 0;
  112. }
  113. // Scale by distance
  114. double thisSpGain = gain * ((radius - dis) / radius);
  115. sp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  116. ownerID, (float)thisSpGain, flags);
  117. });
  118. }
  119. public virtual void TriggerSound(
  120. UUID soundId, UUID ownerID, UUID objectID, UUID parentID, double gain, Vector3 position, UInt64 handle, float radius)
  121. {
  122. SceneObjectPart part;
  123. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  124. {
  125. ScenePresence sp;
  126. if (!m_scene.TryGetScenePresence(ownerID, out sp))
  127. return;
  128. }
  129. else
  130. {
  131. SceneObjectGroup grp = part.ParentGroup;
  132. if (grp.IsAttachment && grp.AttachmentPoint > 30)
  133. {
  134. objectID = ownerID;
  135. parentID = ownerID;
  136. }
  137. }
  138. if (radius == 0)
  139. radius = MaxDistance;
  140. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  141. {
  142. double dis = Util.GetDistanceTo(sp.AbsolutePosition, position);
  143. if (dis > MaxDistance) // Max audio distance
  144. return;
  145. // Scale by distance
  146. double thisSpGain = gain * ((radius - dis) / radius);
  147. sp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  148. objectID, parentID, handle, position,
  149. (float)thisSpGain);
  150. });
  151. }
  152. public virtual void StopSound(UUID objectID)
  153. {
  154. SceneObjectPart m_host;
  155. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  156. return;
  157. StopSound(m_host);
  158. }
  159. private static void StopSound(SceneObjectPart m_host)
  160. {
  161. m_host.AdjustSoundGain(0);
  162. // Xantor 20080528: Clear prim data of sound instead
  163. if (m_host.ParentGroup.LoopSoundSlavePrims.Contains(m_host))
  164. {
  165. if (m_host.ParentGroup.LoopSoundMasterPrim == m_host)
  166. {
  167. foreach (SceneObjectPart part in m_host.ParentGroup.LoopSoundSlavePrims)
  168. {
  169. part.Sound = UUID.Zero;
  170. part.SoundFlags = 1 << 5;
  171. part.SoundRadius = 0;
  172. part.ScheduleFullUpdate();
  173. part.SendFullUpdateToAllClients();
  174. }
  175. m_host.ParentGroup.LoopSoundMasterPrim = null;
  176. m_host.ParentGroup.LoopSoundSlavePrims.Clear();
  177. }
  178. else
  179. {
  180. m_host.Sound = UUID.Zero;
  181. m_host.SoundFlags = 1 << 5;
  182. m_host.SoundRadius = 0;
  183. m_host.ScheduleFullUpdate();
  184. m_host.SendFullUpdateToAllClients();
  185. }
  186. }
  187. else
  188. {
  189. m_host.Sound = UUID.Zero;
  190. m_host.SoundFlags = 1 << 5;
  191. m_host.SoundRadius = 0;
  192. m_host.ScheduleFullUpdate();
  193. m_host.SendFullUpdateToAllClients();
  194. }
  195. }
  196. public virtual void PreloadSound(UUID objectID, UUID soundID, float radius)
  197. {
  198. SceneObjectPart part;
  199. if (soundID == UUID.Zero
  200. || !m_scene.TryGetSceneObjectPart(objectID, out part))
  201. {
  202. return;
  203. }
  204. if (radius == 0)
  205. radius = MaxDistance;
  206. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  207. {
  208. if (!(Util.GetDistanceTo(sp.AbsolutePosition, part.AbsolutePosition) >= MaxDistance))
  209. sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
  210. });
  211. }
  212. // Xantor 20080528 we should do this differently.
  213. // 1) apply the sound to the object
  214. // 2) schedule full update
  215. // just sending the sound out once doesn't work so well when other avatars come in view later on
  216. // or when the prim gets moved, changed, sat on, whatever
  217. // see large number of mantises (mantes?)
  218. // 20080530 Updated to remove code duplication
  219. // 20080530 Stop sound if there is one, otherwise volume only changes don't work
  220. public void LoopSound(UUID objectID, UUID soundID,
  221. double volume, double radius, bool isMaster)
  222. {
  223. SceneObjectPart m_host;
  224. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  225. return;
  226. if (isMaster)
  227. m_host.ParentGroup.LoopSoundMasterPrim = m_host;
  228. if (m_host.Sound != UUID.Zero)
  229. StopSound(m_host);
  230. m_host.Sound = soundID;
  231. m_host.SoundGain = volume;
  232. m_host.SoundFlags = 1; // looping
  233. m_host.SoundRadius = radius;
  234. m_host.ScheduleFullUpdate();
  235. m_host.SendFullUpdateToAllClients();
  236. }
  237. public void SendSound(UUID objectID, UUID soundID, double volume,
  238. bool triggered, byte flags, float radius, bool useMaster,
  239. bool isMaster)
  240. {
  241. if (soundID == UUID.Zero)
  242. return;
  243. SceneObjectPart part;
  244. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  245. return;
  246. volume = Util.Clip((float)volume, 0, 1);
  247. UUID parentID = part.ParentGroup.UUID;
  248. Vector3 position = part.AbsolutePosition; // region local
  249. ulong regionHandle = m_scene.RegionInfo.RegionHandle;
  250. if (useMaster)
  251. {
  252. if (isMaster)
  253. {
  254. if (triggered)
  255. TriggerSound(soundID, part.OwnerID, part.UUID, parentID, volume, position, regionHandle, radius);
  256. else
  257. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, flags, radius);
  258. part.ParentGroup.PlaySoundMasterPrim = part;
  259. if (triggered)
  260. TriggerSound(soundID, part.OwnerID, part.UUID, parentID, volume, position, regionHandle, radius);
  261. else
  262. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, flags, radius);
  263. foreach (SceneObjectPart prim in part.ParentGroup.PlaySoundSlavePrims)
  264. {
  265. position = prim.AbsolutePosition; // region local
  266. if (triggered)
  267. TriggerSound(soundID, part.OwnerID, prim.UUID, parentID, volume, position, regionHandle, radius);
  268. else
  269. PlayAttachedSound(soundID, part.OwnerID, prim.UUID, volume, position, flags, radius);
  270. }
  271. part.ParentGroup.PlaySoundSlavePrims.Clear();
  272. part.ParentGroup.PlaySoundMasterPrim = null;
  273. }
  274. else
  275. {
  276. part.ParentGroup.PlaySoundSlavePrims.Add(part);
  277. }
  278. }
  279. else
  280. {
  281. if (triggered)
  282. TriggerSound(soundID, part.OwnerID, part.UUID, parentID, volume, position, regionHandle, radius);
  283. else
  284. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, flags, radius);
  285. }
  286. }
  287. public void TriggerSoundLimited(UUID objectID, UUID sound,
  288. double volume, Vector3 min, Vector3 max)
  289. {
  290. if (sound == UUID.Zero)
  291. return;
  292. SceneObjectPart part;
  293. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  294. return;
  295. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  296. {
  297. double dis = Util.GetDistanceTo(sp.AbsolutePosition,
  298. part.AbsolutePosition);
  299. if (dis > MaxDistance) // Max audio distance
  300. return;
  301. else if (!Util.IsInsideBox(sp.AbsolutePosition, min, max))
  302. return;
  303. // Scale by distance
  304. double thisSpGain = volume * ((MaxDistance - dis) / MaxDistance);
  305. sp.ControllingClient.SendTriggeredSound(sound, part.OwnerID,
  306. part.UUID, part.ParentGroup.UUID,
  307. m_scene.RegionInfo.RegionHandle,
  308. part.AbsolutePosition, (float)thisSpGain);
  309. });
  310. }
  311. public void SetSoundQueueing(UUID objectID, bool shouldQueue)
  312. {
  313. SceneObjectPart part;
  314. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  315. return;
  316. part.SoundQueueing = shouldQueue;
  317. }
  318. #endregion
  319. }
  320. }