SoundModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, float radius)
  105. {
  106. SceneObjectPart part;
  107. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  108. return;
  109. SceneObjectGroup grp = part.ParentGroup;
  110. if (radius == 0)
  111. radius = MaxDistance;
  112. if (part.SoundQueueing)
  113. flags |= (byte)SoundFlags.QUEUE;
  114. if (grp.IsAttachment)
  115. {
  116. ScenePresence ssp = null;
  117. if (!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  118. return;
  119. if (!ssp.ParcelAllowThisAvatarSounds)
  120. return;
  121. if (grp.HasPrivateAttachmentPoint)
  122. {
  123. ssp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  124. ownerID, (float)gain, flags);
  125. return;
  126. }
  127. }
  128. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  129. {
  130. sp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  131. ownerID, (float)gain, flags);
  132. });
  133. }
  134. public virtual void TriggerSound(
  135. UUID soundId, UUID ownerID, UUID objectID, UUID parentID, double gain, Vector3 position, UInt64 handle, float radius)
  136. {
  137. SceneObjectPart part;
  138. ScenePresence ssp = null;
  139. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  140. {
  141. if (!m_scene.TryGetScenePresence(ownerID, out ssp))
  142. return;
  143. if (!ssp.ParcelAllowThisAvatarSounds)
  144. return;
  145. }
  146. else
  147. {
  148. SceneObjectGroup grp = part.ParentGroup;
  149. if (grp.IsAttachment)
  150. {
  151. if (!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  152. return;
  153. if (!ssp.ParcelAllowThisAvatarSounds)
  154. return;
  155. if (grp.HasPrivateAttachmentPoint)
  156. {
  157. ssp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  158. objectID, parentID, handle, position,
  159. (float)gain);
  160. return;
  161. }
  162. }
  163. }
  164. if (radius == 0)
  165. radius = MaxDistance;
  166. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  167. {
  168. double dis = Util.GetDistanceTo(sp.AbsolutePosition, position);
  169. if (dis > radius) // Max audio distance
  170. return;
  171. sp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  172. objectID, parentID, handle, position,
  173. (float)gain);
  174. });
  175. }
  176. public virtual void StopSound(UUID objectID)
  177. {
  178. SceneObjectPart m_host;
  179. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  180. return;
  181. StopSound(m_host);
  182. }
  183. private static void StopSound(SceneObjectPart m_host)
  184. {
  185. // m_host.AdjustSoundGain(0);
  186. m_host.Sound = UUID.Zero;
  187. m_host.SoundFlags = (byte)SoundFlags.STOP;
  188. m_host.SoundRadius = 0;
  189. m_host.SoundGain = 0;
  190. m_host.ScheduleFullUpdate();
  191. m_host.SendFullUpdateToAllClients();
  192. }
  193. public virtual void PreloadSound(UUID objectID, UUID soundID, float radius)
  194. {
  195. SceneObjectPart part;
  196. if (soundID == UUID.Zero
  197. || !m_scene.TryGetSceneObjectPart(objectID, out part))
  198. {
  199. return;
  200. }
  201. if (radius == 0)
  202. radius = MaxDistance;
  203. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  204. {
  205. if (Util.GetDistanceTo(sp.AbsolutePosition, part.AbsolutePosition) < radius)
  206. sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
  207. });
  208. }
  209. // Xantor 20080528 we should do this differently.
  210. // 1) apply the sound to the object
  211. // 2) schedule full update
  212. // just sending the sound out once doesn't work so well when other avatars come in view later on
  213. // or when the prim gets moved, changed, sat on, whatever
  214. // see large number of mantises (mantes?)
  215. // 20080530 Updated to remove code duplication
  216. // 20080530 Stop sound if there is one, otherwise volume only changes don't work
  217. public void LoopSound(UUID objectID, UUID soundID,
  218. double volume, double radius, bool isMaster, bool isSlave)
  219. {
  220. SceneObjectPart m_host;
  221. if (!m_scene.TryGetSceneObjectPart(objectID, out m_host))
  222. return;
  223. byte iflags = 1; // looping
  224. if (isMaster)
  225. iflags |= (byte)SoundFlags.SYNC_MASTER;
  226. // TODO check viewer seems to accept both
  227. if (isSlave)
  228. iflags |= (byte)SoundFlags.SYNC_SLAVE;
  229. if (m_host.SoundQueueing)
  230. iflags |= (byte)SoundFlags.QUEUE;
  231. m_host.Sound = soundID;
  232. m_host.SoundGain = volume;
  233. m_host.SoundFlags = iflags;
  234. m_host.SoundRadius = radius;
  235. m_host.ScheduleFullUpdate();
  236. m_host.SendFullUpdateToAllClients();
  237. }
  238. public void SendSound(UUID objectID, UUID soundID, double volume,
  239. bool triggered, byte flags, float radius, bool useMaster,
  240. bool isMaster)
  241. {
  242. if (soundID == UUID.Zero)
  243. return;
  244. SceneObjectPart part;
  245. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  246. return;
  247. volume = Util.Clip((float)volume, 0, 1);
  248. UUID parentID = part.ParentGroup.UUID;
  249. Vector3 position = part.AbsolutePosition; // region local
  250. ulong regionHandle = m_scene.RegionInfo.RegionHandle;
  251. if(triggered)
  252. TriggerSound(soundID, part.OwnerID, part.UUID, parentID, volume, position, regionHandle, radius);
  253. else
  254. {
  255. byte bflags = 0;
  256. if (isMaster)
  257. bflags |= (byte)SoundFlags.SYNC_MASTER;
  258. // TODO check viewer seems to accept both
  259. if (useMaster)
  260. bflags |= (byte)SoundFlags.SYNC_SLAVE;
  261. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, bflags, radius);
  262. }
  263. }
  264. public void TriggerSoundLimited(UUID objectID, UUID sound,
  265. double volume, Vector3 min, Vector3 max)
  266. {
  267. if (sound == UUID.Zero)
  268. return;
  269. SceneObjectPart part;
  270. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  271. return;
  272. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  273. {
  274. double dis = Util.GetDistanceTo(sp.AbsolutePosition,
  275. part.AbsolutePosition);
  276. if (dis > MaxDistance) // Max audio distance
  277. return;
  278. else if (!Util.IsInsideBox(sp.AbsolutePosition, min, max))
  279. return;
  280. // Scale by distance
  281. double thisSpGain = volume * ((MaxDistance - dis) / MaxDistance);
  282. sp.ControllingClient.SendTriggeredSound(sound, part.OwnerID,
  283. part.UUID, part.ParentGroup.UUID,
  284. m_scene.RegionInfo.RegionHandle,
  285. part.AbsolutePosition, (float)thisSpGain);
  286. });
  287. }
  288. public void SetSoundQueueing(UUID objectID, bool shouldQueue)
  289. {
  290. SceneObjectPart part;
  291. if (!m_scene.TryGetSceneObjectPart(objectID, out part))
  292. return;
  293. part.SoundQueueing = shouldQueue;
  294. }
  295. #endregion
  296. }
  297. }