LIBOMV-81 Adds methods for playing sound files to SoundManager

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@1899 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
Jim Radford
2008-06-25 17:47:59 +00:00
parent 5c19a8883e
commit 9cbc2d010c

View File

@@ -22,93 +22,160 @@
* 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 libsecondlife.Packets;
namespace libsecondlife
{
public class SoundManager
{
public readonly SecondLife Client;
public delegate void AttachSoundCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID, float gain, byte flags);
public delegate void AttachedSoundGainChangeCallback(LLUUID objectID, float gain);
public delegate void SoundTriggerCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID, LLUUID parentID, float gain, ulong regionHandle, LLVector3 position);
public delegate void PreloadSoundCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID);
public event AttachSoundCallback OnAttachSound;
public event AttachedSoundGainChangeCallback OnAttachSoundGainChange;
public event SoundTriggerCallback OnSoundTrigger;
public event PreloadSoundCallback OnPreloadSound;
public SoundManager(SecondLife 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));
}
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); }
}
}
}
}
*/
using System;
using System.Collections.Generic;
using libsecondlife.Packets;
namespace libsecondlife
{
public class SoundManager
{
public readonly SecondLife Client;
public delegate void AttachSoundCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID, float gain, byte flags);
public delegate void AttachedSoundGainChangeCallback(LLUUID objectID, float gain);
public delegate void SoundTriggerCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID, LLUUID parentID, float gain, ulong regionHandle, LLVector3 position);
public delegate void PreloadSoundCallback(LLUUID soundID, LLUUID ownerID, LLUUID objectID);
public event AttachSoundCallback OnAttachSound;
public event AttachedSoundGainChangeCallback OnAttachSoundGainChange;
public event SoundTriggerCallback OnSoundTrigger;
public event PreloadSoundCallback OnPreloadSound;
public SoundManager(SecondLife 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
/// <summary>
/// Plays a sound in the current region at full volume from avatar position
/// </summary>
/// <param name="soundID">UUID of the sound to be played</param>
public void SoundTrigger(LLUUID soundID)
{
SoundTrigger(soundID, Client.Self.SimPosition, 1.0f);
}
/// <summary>
/// Plays a sound in the current region at full volume
/// </summary>
/// <param name="soundID">UUID of the sound to be played.</param>
/// <param name="position">position for the sound to be played at. Normally the avatar.</param>
public void SoundTrigger(LLUUID soundID, LLVector3 position)
{
SoundTrigger(soundID, Client.Self.SimPosition, 1.0f);
}
/// <summary>
/// Plays a sound in the current region
/// </summary>
/// <param name="soundID">UUID of the sound to be played.</param>
/// <param name="position">position for the sound to be played at. Normally the avatar.</param>
/// <param name="gain">volume of the sound, from 0.0 to 1.0</param>
public void SoundTrigger(LLUUID soundID, LLVector3 position, float gain)
{
SoundTrigger(soundID, Client.Network.CurrentSim.Handle, position, 1.0f);
}
/// <summary>
/// Plays a sound in the specified sim
/// </summary>
/// <param name="soundID">UUID of the sound to be played.</param>
/// <param name="sim">UUID of the sound to be played.</param>
/// <param name="position">position for the sound to be played at. Normally the avatar.</param>
/// <param name="gain">volume of the sound, from 0.0 to 1.0</param>
public void SoundTrigger(LLUUID soundID, Simulator sim, LLVector3 position, float gain)
{
SoundTrigger(soundID, sim.Handle, position, 1.0f);
}
/// <summary>
/// Plays a sound
/// </summary>
/// <param name="soundID">UUID of the sound to be played.</param>
/// <param name="handle">handle id for the sim to be played in.</param>
/// <param name="position">position for the sound to be played at. Normally the avatar.</param>
/// <param name="gain">volume of the sound, from 0.0 to 1.0</param>
public void SoundTrigger(LLUUID soundID, ulong handle, LLVector3 position, float gain)
{
SoundTriggerPacket soundtrigger = new SoundTriggerPacket();
soundtrigger.SoundData = new SoundTriggerPacket.SoundDataBlock();
soundtrigger.SoundData.SoundID = soundID;
soundtrigger.SoundData.ObjectID = LLUUID.Zero;
soundtrigger.SoundData.OwnerID = LLUUID.Zero;
soundtrigger.SoundData.ParentID = LLUUID.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
}
}