SoundModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 enum SoundFlags: byte
  47. {
  48. NONE = 0,
  49. LOOP = 1 << 0,
  50. SYNC_MASTER = 1<<1,
  51. SYNC_SLAVE = 1<<2,
  52. SYNC_PENDING = 1<<3,
  53. QUEUE = 1<<4,
  54. STOP = 1<<5,
  55. SYNC_MASK = SYNC_MASTER | SYNC_SLAVE | SYNC_PENDING
  56. }
  57. public bool Enabled { get; private set; }
  58. public float MaxDistance { get; private set; }
  59. #region INonSharedRegionModule
  60. public void Initialise(IConfigSource configSource)
  61. {
  62. IConfig config = configSource.Configs["Sounds"];
  63. if (config == null)
  64. {
  65. Enabled = true;
  66. MaxDistance = 100.0f;
  67. }
  68. else
  69. {
  70. Enabled = config.GetString("Module", "OpenSim.Region.CoreModules.dll:SoundModule") ==
  71. Path.GetFileName(Assembly.GetExecutingAssembly().Location)
  72. + ":" + MethodBase.GetCurrentMethod().DeclaringType.Name;
  73. MaxDistance = config.GetFloat("MaxDistance", 100.0f);
  74. }
  75. }
  76. public void AddRegion(Scene scene) { }
  77. public void RemoveRegion(Scene scene)
  78. {
  79. m_scene.EventManager.OnNewClient -= OnNewClient;
  80. }
  81. public void RegionLoaded(Scene scene)
  82. {
  83. if (!Enabled)
  84. return;
  85. m_scene = scene;
  86. m_scene.EventManager.OnNewClient += OnNewClient;
  87. m_scene.RegisterModuleInterface<ISoundModule>(this);
  88. }
  89. public void Close() { }
  90. public Type ReplaceableInterface
  91. {
  92. get { return typeof(ISoundModule); }
  93. }
  94. public string Name { get { return "Sound Module"; } }
  95. #endregion
  96. #region Event Handlers
  97. private void OnNewClient(IClientAPI client)
  98. {
  99. client.OnSoundTrigger += TriggerSound;
  100. }
  101. #endregion
  102. #region ISoundModule
  103. public virtual void PlayAttachedSound(
  104. UUID soundID, UUID ownerID, UUID objectID, double gain, Vector3 position, byte flags)
  105. {
  106. SceneObjectPart part;
  107. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  108. return;
  109. if (part.SoundRadius == 0)
  110. part.SoundRadius = MaxDistance;
  111. part.SoundFlags = 0;
  112. if (part.SoundQueueing)
  113. flags |= (byte)SoundFlags.QUEUE;
  114. SceneObjectGroup grp = part.ParentGroup;
  115. if(grp == null | grp.IsDeleted)
  116. return;
  117. if (grp.IsAttachment)
  118. {
  119. ScenePresence ssp = null;
  120. if (!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  121. return;
  122. if (grp.HasPrivateAttachmentPoint)
  123. {
  124. ssp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  125. ownerID, (float)gain, flags);
  126. return;
  127. }
  128. if (!ssp.ParcelAllowThisAvatarSounds)
  129. return;
  130. }
  131. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  132. {
  133. sp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  134. ownerID, (float)gain, flags);
  135. });
  136. }
  137. public virtual void TriggerSound(
  138. UUID soundId, UUID ownerID, UUID objectID, UUID parentID, double gain, Vector3 position, UInt64 handle)
  139. {
  140. float radius;
  141. SceneObjectPart part;
  142. ScenePresence ssp = null;
  143. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  144. {
  145. if (!m_scene.TryGetScenePresence(ownerID, out ssp))
  146. return;
  147. if (!ssp.ParcelAllowThisAvatarSounds)
  148. return;
  149. radius = MaxDistance;
  150. }
  151. else
  152. {
  153. SceneObjectGroup grp = part.ParentGroup;
  154. if(grp.IsAttachment)
  155. {
  156. if(!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  157. return;
  158. if(!ssp.ParcelAllowThisAvatarSounds)
  159. return;
  160. }
  161. radius = (float)part.SoundRadius;
  162. if(radius == 0)
  163. {
  164. radius = MaxDistance;
  165. part.SoundRadius = MaxDistance;
  166. }
  167. part.SoundFlags = 0;
  168. }
  169. radius *= radius;
  170. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  171. {
  172. if (Vector3.DistanceSquared(sp.AbsolutePosition, position) > radius) // Max audio distance
  173. return;
  174. sp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  175. objectID, parentID, handle, position,
  176. (float)gain);
  177. });
  178. }
  179. public virtual void StopSound(UUID objectID)
  180. {
  181. SceneObjectPart m_host;
  182. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  183. return;
  184. StopSound(m_host);
  185. }
  186. private static void StopSound(SceneObjectPart m_host)
  187. {
  188. m_host.Sound = UUID.Zero;
  189. m_host.SoundFlags = (byte)SoundFlags.STOP;
  190. m_host.SoundGain = 0;
  191. m_host.ScheduleFullUpdate();
  192. m_host.SendFullUpdateToAllClients();
  193. }
  194. public virtual void PreloadSound(UUID objectID, UUID soundID)
  195. {
  196. SceneObjectPart part;
  197. if (soundID == UUID.Zero
  198. || !m_scene.TryGetSceneObjectPart(objectID, out part))
  199. {
  200. return;
  201. }
  202. float radius = (float)part.SoundRadius;
  203. if (radius == 0)
  204. {
  205. radius = MaxDistance;
  206. part.SoundRadius = radius;
  207. }
  208. radius *= 4.0f * radius; // avatars and prims do move
  209. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  210. {
  211. if (Vector3.DistanceSquared(sp.AbsolutePosition, part.AbsolutePosition) < radius)
  212. sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
  213. });
  214. }
  215. // Xantor 20080528 we should do this differently.
  216. // 1) apply the sound to the object
  217. // 2) schedule full update
  218. // just sending the sound out once doesn't work so well when other avatars come in view later on
  219. // or when the prim gets moved, changed, sat on, whatever
  220. // see large number of mantises (mantes?)
  221. // 20080530 Updated to remove code duplication
  222. // 20080530 Stop sound if there is one, otherwise volume only changes don't work
  223. public void LoopSound(UUID objectID, UUID soundID,
  224. double volume, bool isMaster, bool isSlave)
  225. {
  226. SceneObjectPart m_host;
  227. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  228. return;
  229. byte iflags = 1; // looping
  230. if (isMaster)
  231. iflags |= (byte)SoundFlags.SYNC_MASTER;
  232. // TODO check viewer seems to accept both
  233. if (isSlave)
  234. iflags |= (byte)SoundFlags.SYNC_SLAVE;
  235. if (m_host.SoundQueueing)
  236. iflags |= (byte)SoundFlags.QUEUE;
  237. m_host.Sound = soundID;
  238. m_host.SoundGain = volume;
  239. m_host.SoundFlags = iflags;
  240. if (m_host.SoundRadius == 0)
  241. m_host.SoundRadius = MaxDistance;
  242. m_host.ScheduleFullUpdate();
  243. m_host.SendFullUpdateToAllClients();
  244. }
  245. public void SendSound(UUID objectID, UUID soundID, double volume,
  246. bool triggered, byte flags, bool useMaster,
  247. bool isMaster)
  248. {
  249. if (soundID == UUID.Zero)
  250. return;
  251. SceneObjectPart part;
  252. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  253. return;
  254. volume = Util.Clip((float)volume, 0, 1);
  255. UUID parentID = part.ParentGroup.UUID;
  256. Vector3 position = part.AbsolutePosition; // region local
  257. ulong regionHandle = m_scene.RegionInfo.RegionHandle;
  258. if(triggered)
  259. TriggerSound(soundID, part.OwnerID, part.UUID, parentID, volume, position, regionHandle);
  260. else
  261. {
  262. byte bflags = 0;
  263. if (isMaster)
  264. bflags |= (byte)SoundFlags.SYNC_MASTER;
  265. // TODO check viewer seems to accept both
  266. if (useMaster)
  267. bflags |= (byte)SoundFlags.SYNC_SLAVE;
  268. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, bflags);
  269. }
  270. }
  271. public void TriggerSoundLimited(UUID objectID, UUID sound,
  272. double volume, Vector3 min, Vector3 max)
  273. {
  274. if (sound == UUID.Zero)
  275. return;
  276. SceneObjectPart part;
  277. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  278. return;
  279. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  280. {
  281. double dis = Util.GetDistanceTo(sp.AbsolutePosition,
  282. part.AbsolutePosition);
  283. if (dis > MaxDistance) // Max audio distance
  284. return;
  285. else if (!Util.IsInsideBox(sp.AbsolutePosition, min, max))
  286. return;
  287. // Scale by distance
  288. double thisSpGain = volume * ((MaxDistance - dis) / MaxDistance);
  289. sp.ControllingClient.SendTriggeredSound(sound, part.OwnerID,
  290. part.UUID, part.ParentGroup.UUID,
  291. m_scene.RegionInfo.RegionHandle,
  292. part.AbsolutePosition, (float)thisSpGain);
  293. });
  294. }
  295. public void SetSoundQueueing(UUID objectID, bool shouldQueue)
  296. {
  297. SceneObjectPart part;
  298. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  299. return;
  300. part.SoundQueueing = shouldQueue;
  301. }
  302. #endregion
  303. }
  304. }