/* * Copyright (c) 2006-2008, openmetaverse.org * All rights reserved. * * - Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * - Neither the name of the openmetaverse.org nor the names * of its contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Collections.Generic; using OpenMetaverse.Packets; namespace OpenMetaverse { public class SoundManager { public readonly GridClient Client; public delegate void AttachSoundCallback(UUID soundID, UUID ownerID, UUID objectID, float gain, byte flags); public delegate void AttachedSoundGainChangeCallback(UUID objectID, float gain); public delegate void SoundTriggerCallback(UUID soundID, UUID ownerID, UUID objectID, UUID parentID, float gain, ulong regionHandle, Vector3 position); public delegate void PreloadSoundCallback(UUID soundID, UUID ownerID, UUID objectID); public event AttachSoundCallback OnAttachSound; public event AttachedSoundGainChangeCallback OnAttachSoundGainChange; public event SoundTriggerCallback OnSoundTrigger; public event PreloadSoundCallback OnPreloadSound; public SoundManager(GridClient client) { Client = client; Client.Network.RegisterCallback(PacketType.AttachedSound, new NetworkManager.PacketCallback(AttachedSoundHandler)); Client.Network.RegisterCallback(PacketType.AttachedSoundGainChange, new NetworkManager.PacketCallback(AttachedSoundGainChangeHandler)); Client.Network.RegisterCallback(PacketType.PreloadSound, new NetworkManager.PacketCallback(PreloadSoundHandler)); Client.Network.RegisterCallback(PacketType.SoundTrigger, new NetworkManager.PacketCallback(SoundTriggerHandler)); } #region public methods /// /// Plays a sound in the current region at full volume from avatar position /// /// UUID of the sound to be played public void SoundTrigger(UUID soundID) { SoundTrigger(soundID, Client.Self.SimPosition, 1.0f); } /// /// Plays a sound in the current region at full volume /// /// UUID of the sound to be played. /// position for the sound to be played at. Normally the avatar. public void SoundTrigger(UUID soundID, Vector3 position) { SoundTrigger(soundID, Client.Self.SimPosition, 1.0f); } /// /// Plays a sound in the current region /// /// UUID of the sound to be played. /// position for the sound to be played at. Normally the avatar. /// volume of the sound, from 0.0 to 1.0 public void SoundTrigger(UUID soundID, Vector3 position, float gain) { SoundTrigger(soundID, Client.Network.CurrentSim.Handle, position, 1.0f); } /// /// Plays a sound in the specified sim /// /// UUID of the sound to be played. /// UUID of the sound to be played. /// position for the sound to be played at. Normally the avatar. /// volume of the sound, from 0.0 to 1.0 public void SoundTrigger(UUID soundID, Simulator sim, Vector3 position, float gain) { SoundTrigger(soundID, sim.Handle, position, 1.0f); } /// /// Plays a sound /// /// UUID of the sound to be played. /// handle id for the sim to be played in. /// position for the sound to be played at. Normally the avatar. /// volume of the sound, from 0.0 to 1.0 public void SoundTrigger(UUID soundID, ulong handle, Vector3 position, float gain) { SoundTriggerPacket soundtrigger = new SoundTriggerPacket(); soundtrigger.SoundData = new SoundTriggerPacket.SoundDataBlock(); soundtrigger.SoundData.SoundID = soundID; soundtrigger.SoundData.ObjectID = UUID.Zero; soundtrigger.SoundData.OwnerID = UUID.Zero; soundtrigger.SoundData.ParentID = UUID.Zero; soundtrigger.SoundData.Handle = handle; soundtrigger.SoundData.Position = position; soundtrigger.SoundData.Gain = gain; Client.Network.SendPacket(soundtrigger); } #endregion #region Packet Handlers protected void AttachedSoundHandler(Packet packet, Simulator simulator) { AttachedSoundPacket sound = (AttachedSoundPacket)packet; if (OnAttachSound != null) { try { OnAttachSound(sound.DataBlock.SoundID, sound.DataBlock.OwnerID, sound.DataBlock.ObjectID, sound.DataBlock.Gain, sound.DataBlock.Flags); } catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } } } protected void AttachedSoundGainChangeHandler(Packet packet, Simulator simulator) { AttachedSoundGainChangePacket change = (AttachedSoundGainChangePacket)packet; if (OnAttachSoundGainChange != null) { try { OnAttachSoundGainChange(change.DataBlock.ObjectID, change.DataBlock.Gain); } catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } } } protected void PreloadSoundHandler(Packet packet, Simulator simulator) { PreloadSoundPacket preload = (PreloadSoundPacket)packet; if (OnPreloadSound != null) { foreach (PreloadSoundPacket.DataBlockBlock data in preload.DataBlock) { try { OnPreloadSound(data.SoundID, data.OwnerID, data.ObjectID); } catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } } } } protected void SoundTriggerHandler(Packet packet, Simulator simulator) { SoundTriggerPacket trigger = (SoundTriggerPacket)packet; if (OnSoundTrigger != null) { try { OnSoundTrigger( trigger.SoundData.SoundID, trigger.SoundData.OwnerID, trigger.SoundData.ObjectID, trigger.SoundData.ParentID, trigger.SoundData.Gain, trigger.SoundData.Handle, trigger.SoundData.Position ); } catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); } } } #endregion } }