OSDN Git Service

Added debug logging messages, and sound cleanup
authorMojito Sorbet <mojitotech@gmail.com>
Fri, 28 May 2010 03:50:04 +0000 (03:50 +0000)
committerMojito Sorbet <mojitotech@gmail.com>
Fri, 28 May 2010 03:50:04 +0000 (03:50 +0000)
git-svn-id: https://radegast.googlecode.com/svn/branches/sounds@654 f7a694da-4d33-11de-9ad6-1127a62b9fcd

Radegast/Core/Media/BufferSound.cs
Radegast/Core/Media/MediaManager.cs
Radegast/Core/Media/MediaObject.cs

index ad7131b..cfc3bb4 100644 (file)
@@ -66,7 +66,8 @@ namespace Radegast.Media
         private void InitBuffer(UUID soundId, bool loop, bool global, Vector3 worldpos, float vol)\r
         {\r
             // Do not let this get garbage-collected.\r
-            allBuffers.AddLast(this);\r
+            lock (allBuffers)\r
+                allBuffers.Add(soundId,this);\r
 \r
             Id = soundId;\r
             position = FromOMVSpace(worldpos);\r
@@ -93,10 +94,20 @@ namespace Radegast.Media
                 new AssetManager.AssetReceivedCallback(Assets_OnSoundReceived));\r
         }\r
 \r
+        public static void Kill( UUID id )\r
+        {\r
+            if (allBuffers.ContainsKey(id))\r
+            {\r
+                BufferSound bs = allBuffers[id];\r
+                bs.StopSound();\r
+            }\r
+        }\r
+\r
         public BufferSound( UUID soundId )\r
             : base()\r
         {\r
-            allBuffers.AddLast(this);\r
+            lock (allBuffers)\r
+                allBuffers.Add(soundId,this);\r
             prefetchOnly = true;\r
             Id = soundId;\r
 \r
@@ -125,10 +136,12 @@ namespace Radegast.Media
                 // If this was a Prefetch, just stop here.\r
                 if (prefetchOnly)\r
                 {\r
-                    allBuffers.Remove(this);\r
+                    allBuffers.Remove(Id);\r
                     return;\r
                 }\r
 \r
+                Logger.Log("Opening sound " + Id.ToString(), Helpers.LogLevel.Debug);\r
+\r
                 // Decode the Ogg Vorbis buffer.\r
                 AssetSound s = asset as AssetSound;\r
                 s.Decode();\r
@@ -185,7 +198,7 @@ namespace Radegast.Media
                             1.2f,       // Any closer than this gets no louder\r
                             20.0f));     // Further than this gets no softer.\r
 \r
-                // Set the sound point of origin.\r
+                // Set the sound point of origin.  This is in GLOBAL coordinates.\r
                 FMODExec(channel.set3DAttributes(ref position, ref ZeroVector));\r
 \r
                 // Turn off pause mode.  The sound will start playing now.\r
@@ -193,7 +206,7 @@ namespace Radegast.Media
             }\r
             catch (Exception ex)\r
             {\r
-                Logger.Log("Error starting sound: ", Helpers.LogLevel.Debug, ex);\r
+                Logger.Log("Error starting sound: ", Helpers.LogLevel.Error, ex);\r
             }\r
 \r
             return RESULT.OK;\r
@@ -216,8 +229,21 @@ namespace Radegast.Media
             // Ignore other callback types.\r
             if (type != CHANNEL_CALLBACKTYPE.END) return RESULT.OK;\r
 \r
-            // Release the buffer to avoid a big memory leak.\r
+            StopSound();\r
 \r
+            return RESULT.OK;\r
+        }\r
+\r
+        protected void StopSound()\r
+        {\r
+            Logger.Log("Removing sound " + Id.ToString(), Helpers.LogLevel.Debug);\r
+\r
+            // Release the buffer to avoid a big memory leak.\r
+            if (channel != null)\r
+            {\r
+                channel.stop();\r
+                channel = null;\r
+            }\r
             if (sound != null)\r
             {\r
                 sound.release();\r
@@ -225,9 +251,8 @@ namespace Radegast.Media
             }\r
             channel = null;\r
 \r
-            allBuffers.Remove(this);\r
-\r
-            return RESULT.OK;\r
+            lock (allBuffers)\r
+                allBuffers.Remove(Id);\r
         }\r
 \r
     }\r
index 91b88ad..c7232d4 100644 (file)
@@ -59,7 +59,7 @@ namespace Radegast.Media
 
             loadCallback = new FMOD.SOUND_NONBLOCKCALLBACK(DispatchNonBlockCallback);
             endCallback = new FMOD.CHANNEL_CALLBACK(DispatchEndCallback);
-            allBuffers = new LinkedList<BufferSound>();
+            allBuffers = new Dictionary<UUID,BufferSound>();
 
             // Start the background thread that does all the FMOD calls.
             soundThread = new Thread(new ThreadStart(CommandLoop));
@@ -253,18 +253,21 @@ namespace Radegast.Media
 
             while (true)
             {
+                // Two updates per second.
                 Thread.Sleep(500);
 
                 if (system == null) continue;
 
                 AgentManager my = Instance.Client.Self;
 
+                Vector3 newPosition = new Vector3(my.GlobalPosition);
+
                 // If we are standing still, nothing to update now, but
                 // FMOD needs a 'tick' anyway for callbacks, etc.  In looping
                 // 'game' programs, the loop is the 'tick'.   Since Radegast
                 // uses events and has no loop, we use this position update
                 // thread to drive the FMOD tick.
-                if (my.SimPosition.Equals(lastpos) &&
+                if (newPosition.Equals(lastpos) &&
                     my.Movement.BodyRotation.W == lastface)
                 {
                     invoke(new SoundDelegate(delegate
@@ -274,7 +277,8 @@ namespace Radegast.Media
                     continue;
                 }
 
-                lastpos = my.SimPosition;
+                // We have moved or turned.  Remember new position.
+                lastpos = newPosition;
                 lastface = my.Movement.BodyRotation.W;
 
                 // Convert coordinate spaces.
@@ -286,9 +290,11 @@ namespace Radegast.Media
                 // By definition, facing.W = Cos( angle/2 )
                 double angle = 2.0 * Math.Acos(my.Movement.BodyRotation.W);
                 FMOD.VECTOR forward = new FMOD.VECTOR();
-                forward.x = (float)Math.Asin(angle);
+                forward.x = (float)Math.Sin(angle);
                 forward.y = 0.0f;
-                forward.z = (float)Math.Acos(angle);
+                forward.z = (float)Math.Cos(angle);
+                int facing = (int)(angle * 180.0 / 3.141592);
+                Logger.Log("Facing "+facing.ToString(), Helpers.LogLevel.Debug);
 
                 // Tell FMOD the new orientation.
                 invoke( new SoundDelegate( delegate
@@ -305,11 +311,17 @@ namespace Radegast.Media
             }
         }
 
-        /**
-         * Handle triggering a sound to play
-         */
+        /// <summary>
+        /// Handle request to play a sound, which might (or mioght not) have been preloaded.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
         private void Sound_SoundTrigger(object sender, SoundTriggerEventArgs e)
         {
+            Logger.Log("Trigger sound " + e.SoundID.ToString() +
+                " in object " + e.ObjectID.ToString(),
+                Helpers.LogLevel.Debug);
+
             new BufferSound(
                 e.SoundID,
                 false,
@@ -323,14 +335,26 @@ namespace Radegast.Media
          
         }
 
-        /**
-         * Handle request to preload a sound resource.
-         */
+        
+        /// <summary>
+        /// Handle request to preload a sound for playing later.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
         private void Sound_PreloadSound(object sender, PreloadSoundEventArgs e)
         {
+            Logger.Log("Prefetch sound " + e.SoundID.ToString() +
+                " in object " + e.ObjectID.ToString(),
+                Helpers.LogLevel.Debug);
+
             new BufferSound( e.SoundID );
         }
 
+        /// <summary>
+        /// Handle object updates, looking for sound events
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
         private void Objects_ObjectUpdate(object sender, PrimEventArgs e)
         {
             // Objects without sounds are not interesting.
@@ -339,8 +363,14 @@ namespace Radegast.Media
            
             if ((e.Prim.SoundFlags & SoundFlags.Stop) == SoundFlags.Stop)
             {
+                BufferSound.Kill(e.Prim.Sound);
+                return;
             }
 
+            Logger.Log("Object sound " + e.Prim.Sound.ToString() +
+                " in object " + e.Prim.ID.ToString(),
+                Helpers.LogLevel.Debug);
+
             Vector3 fullPosition = e.Prim.Position;
             if (e.Prim.ParentID != 0)
             {
@@ -369,18 +399,6 @@ namespace Radegast.Media
         }
     }
 
-    public class PlayingSound
-    {
-        private UUID Id;
-        private Vector3 position;
-        private float volume;
-
-        public PlayingSound( UUID soundId )
-        {
-            Id = soundId;
-        }
-    }
-
     public class MediaException : Exception
     {
         public MediaException(string msg)
index 61fb8ae..3bb9782 100644 (file)
@@ -54,7 +54,8 @@ namespace Radegast.Media
         ///
         protected static Queue<SoundDelegate> queue;
         protected static MediaManager manager;
-        protected static LinkedList<BufferSound> allBuffers;
+        protected static Dictionary<UUID,BufferSound> allBuffers;
+
 
         // A SOUND represents the data (buffer or stream)
         public FMOD.Sound FMODSound { get { return sound; } }