SoundModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.Reflection;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Mono.Addins;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.CoreModules.World.Sound
  38. {
  39. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SoundModule")]
  40. public class SoundModule : INonSharedRegionModule, ISoundModule
  41. {
  42. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private Scene m_scene;
  44. public enum SoundFlags: byte
  45. {
  46. NONE = 0,
  47. LOOP = 1 << 0,
  48. SYNC_MASTER = 1 << 1,
  49. SYNC_SLAVE = 1 << 2,
  50. SYNC_PENDING = 1 << 3,
  51. QUEUE = 1 << 4,
  52. STOP = 1 << 5,
  53. SYNC_MASK = SYNC_MASTER | SYNC_SLAVE | SYNC_PENDING
  54. }
  55. public bool Enabled { get; private set; }
  56. public float MaxDistance { get; private set; }
  57. #region INonSharedRegionModule
  58. public void Initialise(IConfigSource configSource)
  59. {
  60. IConfig config = configSource.Configs["Sounds"];
  61. if (config == null)
  62. {
  63. Enabled = true;
  64. MaxDistance = 100.0f;
  65. }
  66. else
  67. {
  68. Enabled = config.GetString("Module", "OpenSim.Region.CoreModules.dll:SoundModule") ==
  69. Path.GetFileName(Assembly.GetExecutingAssembly().Location)
  70. + ":" + MethodBase.GetCurrentMethod().DeclaringType.Name;
  71. MaxDistance = config.GetFloat("MaxDistance", 100.0f);
  72. }
  73. }
  74. public void AddRegion(Scene scene) { }
  75. public void RemoveRegion(Scene scene)
  76. {
  77. m_scene.EventManager.OnNewClient -= OnNewClient;
  78. }
  79. public void RegionLoaded(Scene scene)
  80. {
  81. if (!Enabled)
  82. return;
  83. m_scene = scene;
  84. m_scene.EventManager.OnNewClient += OnNewClient;
  85. m_scene.RegisterModuleInterface<ISoundModule>(this);
  86. }
  87. public void Close() { }
  88. public Type ReplaceableInterface
  89. {
  90. get { return typeof(ISoundModule); }
  91. }
  92. public string Name { get { return "Sound Module"; } }
  93. #endregion
  94. #region Event Handlers
  95. private void OnNewClient(IClientAPI client)
  96. {
  97. client.OnSoundTrigger += TriggerSound;
  98. }
  99. #endregion
  100. #region ISoundModule
  101. public virtual void PlayAttachedSound(
  102. UUID soundID, UUID ownerID, UUID objectID, double gain, Vector3 position, byte flags)
  103. {
  104. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  105. return;
  106. if (part.SoundRadius == 0)
  107. part.SoundRadius = MaxDistance;
  108. part.SoundFlags = 0;
  109. if (part.SoundQueueing)
  110. flags |= (byte)SoundFlags.QUEUE;
  111. SceneObjectGroup grp = part.ParentGroup;
  112. if(grp == null | grp.IsDeleted)
  113. return;
  114. if (grp.IsAttachment)
  115. {
  116. ScenePresence ssp = null;
  117. if (!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  118. return;
  119. if (grp.HasPrivateAttachmentPoint)
  120. {
  121. ssp.ControllingClient.SendPlayAttachedSound(soundID, objectID,
  122. ownerID, (float)gain, flags);
  123. return;
  124. }
  125. if (!ssp.ParcelAllowThisAvatarSounds)
  126. return;
  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)
  136. {
  137. float radius;
  138. ScenePresence ssp = null;
  139. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  140. {
  141. if (!m_scene.TryGetScenePresence(ownerID, out ssp))
  142. return;
  143. if (!ssp.ParcelAllowThisAvatarSounds)
  144. return;
  145. radius = MaxDistance;
  146. }
  147. else
  148. {
  149. SceneObjectGroup grp = part.ParentGroup;
  150. if(grp.IsAttachment)
  151. {
  152. if(!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  153. return;
  154. if(!ssp.ParcelAllowThisAvatarSounds)
  155. return;
  156. }
  157. radius = (float)part.SoundRadius;
  158. if(radius == 0)
  159. {
  160. radius = MaxDistance;
  161. part.SoundRadius = MaxDistance;
  162. }
  163. }
  164. radius *= radius;
  165. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  166. {
  167. if (Vector3.DistanceSquared(sp.AbsolutePosition, position) > radius) // Max audio distance
  168. return;
  169. sp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  170. objectID, parentID, handle, position,
  171. (float)gain);
  172. });
  173. }
  174. public virtual void TriggerCollisionSound(
  175. UUID soundId, UUID ownerID, UUID objectID, UUID parentID, double gain, Vector3 position, UInt64 handle)
  176. {
  177. float radius;
  178. ScenePresence ssp = null;
  179. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  180. {
  181. if (!m_scene.TryGetScenePresence(ownerID, out ssp))
  182. return;
  183. if (!ssp.ParcelAllowThisAvatarSounds)
  184. return;
  185. radius = MaxDistance;
  186. }
  187. else
  188. {
  189. SceneObjectGroup grp = part.ParentGroup;
  190. if (grp.IsAttachment)
  191. {
  192. if (!m_scene.TryGetScenePresence(grp.AttachedAvatar, out ssp))
  193. return;
  194. if (!ssp.ParcelAllowThisAvatarSounds)
  195. return;
  196. }
  197. radius = (float)part.SoundRadius;
  198. if (radius == 0)
  199. {
  200. radius = MaxDistance;
  201. part.SoundRadius = MaxDistance;
  202. }
  203. }
  204. radius *= radius;
  205. m_scene.ForEachRootScenePresence(delegate (ScenePresence sp)
  206. {
  207. if(sp.MuteCollisions)
  208. return;
  209. if (Vector3.DistanceSquared(sp.AbsolutePosition, position) > radius) // Max audio distance
  210. return;
  211. sp.ControllingClient.SendTriggeredSound(soundId, ownerID,
  212. objectID, parentID, handle, position,
  213. (float)gain);
  214. });
  215. }
  216. public virtual void StopSound(UUID objectID)
  217. {
  218. if (m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart m_host))
  219. StopSound(m_host);
  220. }
  221. public void StopSound(SceneObjectPart m_host)
  222. {
  223. m_host.Sound = UUID.Zero;
  224. m_host.SoundFlags = (byte)SoundFlags.STOP;
  225. m_host.SoundGain = 0;
  226. m_host.ScheduleFullUpdate();
  227. m_host.SendFullUpdateToAllClients();
  228. }
  229. public virtual void PreloadSound(UUID objectID, UUID soundID)
  230. {
  231. if (soundID.IsZero() || !m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  232. return;
  233. float radius = (float)part.SoundRadius;
  234. if (radius == 0)
  235. {
  236. radius = MaxDistance;
  237. part.SoundRadius = radius;
  238. }
  239. radius *= 4.0f * radius; // avatars and prims do move
  240. m_scene.ForEachRootScenePresence(delegate (ScenePresence sp)
  241. {
  242. if (Vector3.DistanceSquared(sp.AbsolutePosition, part.AbsolutePosition) < radius)
  243. sp.ControllingClient.SendPreLoadSound(objectID, part.OwnerID, soundID);
  244. });
  245. }
  246. public virtual void PreloadSound(SceneObjectPart part, UUID soundID)
  247. {
  248. if (soundID.IsZero())
  249. return;
  250. float radius = (float)part.SoundRadius;
  251. if (radius == 0)
  252. {
  253. radius = MaxDistance;
  254. part.SoundRadius = radius;
  255. }
  256. radius *= 4.0f * radius; // avatars and prims do move
  257. m_scene.ForEachRootScenePresence(delegate (ScenePresence sp)
  258. {
  259. if (Vector3.DistanceSquared(sp.AbsolutePosition, part.AbsolutePosition) < radius)
  260. sp.ControllingClient.SendPreLoadSound(part.UUID, part.OwnerID, soundID);
  261. });
  262. }
  263. // Xantor 20080528 we should do this differently.
  264. // 1) apply the sound to the object
  265. // 2) schedule full update
  266. // just sending the sound out once doesn't work so well when other avatars come in view later on
  267. // or when the prim gets moved, changed, sat on, whatever
  268. // see large number of mantises (mantes?)
  269. // 20080530 Updated to remove code duplication
  270. // 20080530 Stop sound if there is one, otherwise volume only changes don't work
  271. public void LoopSound(UUID objectID, UUID soundID,
  272. double volume, bool isMaster, bool isSlave)
  273. {
  274. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart m_host))
  275. return;
  276. byte iflags = 1; // looping
  277. if (isMaster)
  278. iflags |= (byte)SoundFlags.SYNC_MASTER;
  279. // TODO check viewer seems to accept both
  280. if (isSlave)
  281. iflags |= (byte)SoundFlags.SYNC_SLAVE;
  282. if (m_host.SoundQueueing)
  283. iflags |= (byte)SoundFlags.QUEUE;
  284. m_host.Sound = soundID;
  285. m_host.SoundGain = volume;
  286. m_host.SoundFlags = iflags;
  287. if (m_host.SoundRadius == 0)
  288. m_host.SoundRadius = MaxDistance;
  289. m_host.ScheduleFullUpdate();
  290. m_host.SendFullUpdateToAllClients();
  291. }
  292. public void LoopSound(SceneObjectPart host, UUID soundID,
  293. double volume, bool isMaster, bool isSlave)
  294. {
  295. byte iflags = 1; // looping
  296. if (isMaster)
  297. iflags |= (byte)SoundFlags.SYNC_MASTER;
  298. // TODO check viewer seems to accept both
  299. if (isSlave)
  300. iflags |= (byte)SoundFlags.SYNC_SLAVE;
  301. if (host.SoundQueueing)
  302. iflags |= (byte)SoundFlags.QUEUE;
  303. host.Sound = soundID;
  304. host.SoundGain = volume;
  305. host.SoundFlags = iflags;
  306. if (host.SoundRadius == 0)
  307. host.SoundRadius = MaxDistance;
  308. host.ScheduleFullUpdate();
  309. host.SendFullUpdateToAllClients();
  310. }
  311. public void SendSound(UUID objectID, UUID soundID, double volume,
  312. bool triggered, byte flags, bool useMaster,
  313. bool isMaster)
  314. {
  315. if (soundID.IsZero())
  316. return;
  317. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  318. return;
  319. volume = Utils.Clamp(volume, 0, 1);
  320. Vector3 position = part.AbsolutePosition; // region local
  321. ulong regionHandle = m_scene.RegionInfo.RegionHandle;
  322. if (triggered)
  323. TriggerSound(soundID, part.OwnerID, part.UUID, part.ParentGroup.UUID, volume, position, regionHandle);
  324. else
  325. {
  326. byte bflags = 0;
  327. if (isMaster)
  328. bflags |= (byte)SoundFlags.SYNC_MASTER;
  329. // TODO check viewer seems to accept both
  330. if (useMaster)
  331. bflags |= (byte)SoundFlags.SYNC_SLAVE;
  332. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, bflags);
  333. }
  334. }
  335. public void SendSound(SceneObjectPart part, UUID soundID, double volume,
  336. bool triggered, byte flags, bool useMaster,
  337. bool isMaster)
  338. {
  339. if (soundID.IsZero())
  340. return;
  341. volume = Utils.Clamp(volume, 0, 1);
  342. Vector3 position = part.AbsolutePosition; // region local
  343. ulong regionHandle = m_scene.RegionInfo.RegionHandle;
  344. if (triggered)
  345. TriggerSound(soundID, part.OwnerID, part.UUID, part.ParentGroup.UUID, volume, position, regionHandle);
  346. else
  347. {
  348. byte bflags = 0;
  349. if (isMaster)
  350. bflags |= (byte)SoundFlags.SYNC_MASTER;
  351. // TODO check viewer seems to accept both
  352. if (useMaster)
  353. bflags |= (byte)SoundFlags.SYNC_SLAVE;
  354. PlayAttachedSound(soundID, part.OwnerID, part.UUID, volume, position, bflags);
  355. }
  356. }
  357. public void TriggerSoundLimited(UUID objectID, UUID sound,
  358. double volume, Vector3 min, Vector3 max)
  359. {
  360. if (sound.IsZero())
  361. return;
  362. if (!m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  363. return;
  364. m_scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  365. {
  366. double dis = Util.GetDistanceTo(sp.AbsolutePosition, part.AbsolutePosition);
  367. if (dis > MaxDistance) // Max audio distance
  368. return;
  369. else if (!Util.IsInsideBox(sp.AbsolutePosition, min, max))
  370. return;
  371. // Scale by distance
  372. double thisSpGain = volume * ((MaxDistance - dis) / MaxDistance);
  373. sp.ControllingClient.SendTriggeredSound(sound, part.OwnerID,
  374. part.UUID, part.ParentGroup.UUID,
  375. m_scene.RegionInfo.RegionHandle,
  376. part.AbsolutePosition, (float)thisSpGain);
  377. });
  378. }
  379. public void SetSoundQueueing(UUID objectID, bool shouldQueue)
  380. {
  381. if (m_scene.TryGetSceneObjectPart(objectID, out SceneObjectPart part))
  382. part.SoundQueueing = shouldQueue;
  383. }
  384. #endregion
  385. }
  386. }