diff --git a/src/Audio/AudioCategory.cs b/src/Audio/AudioCategory.cs index c153dc3..4d8491f 100644 --- a/src/Audio/AudioCategory.cs +++ b/src/Audio/AudioCategory.cs @@ -57,7 +57,7 @@ namespace Microsoft.Xna.Framework.Audio private List activeCues; - private Dictionary cueInstanceCounts; + private Dictionary> cueInstanceCounts; private byte maxCueInstances; private MaxInstanceBehavior maxCueBehavior; @@ -80,7 +80,7 @@ namespace Microsoft.Xna.Framework.Audio INTERNAL_name = name; INTERNAL_volume = new PrimitiveInstance(volume); activeCues = new List(); - cueInstanceCounts = new Dictionary(); + cueInstanceCounts = new Dictionary>(); maxCueInstances = maxInstances; maxCueBehavior = (MaxInstanceBehavior) maxBehavior; @@ -131,10 +131,12 @@ namespace Microsoft.Xna.Framework.Audio { Cue curCue = activeCues[0]; curCue.Stop(options); - curCue.SetVariable("NumCueInstances", 0); - cueInstanceCounts[curCue.Name] -= 1; } activeCues.Clear(); + foreach (List count in cueInstanceCounts.Values) + { + count.Clear(); + } } } @@ -190,25 +192,6 @@ namespace Microsoft.Xna.Framework.Audio i -= 1; } } - foreach (Cue curCue in activeCues) - { - curCue.SetVariable( - "NumCueInstances", - cueInstanceCounts[curCue.Name] - ); - } - } - } - - internal void INTERNAL_initCue(Cue newCue) - { - lock (activeCues) - { - if (!cueInstanceCounts.ContainsKey(newCue.Name)) - { - cueInstanceCounts.Add(newCue.Name, 0); - } - newCue.SetVariable("NumCueInstances", cueInstanceCounts[newCue.Name]); } } @@ -252,7 +235,6 @@ namespace Microsoft.Xna.Framework.Audio } if (lowestIndex > -1) { - cueInstanceCounts[activeCues[lowestIndex].Name] -= 1; activeCues[lowestIndex].Stop(AudioStopOptions.AsAuthored); } else @@ -269,8 +251,7 @@ namespace Microsoft.Xna.Framework.Audio } } } - cueInstanceCounts[newCue.Name] += 1; - newCue.SetVariable("NumCueInstances", cueInstanceCounts[newCue.Name]); + cueInstanceCounts[newCue.Name].Add(newCue); activeCues.Add(newCue); } return true; @@ -314,7 +295,6 @@ namespace Microsoft.Xna.Framework.Audio if (lowestIndex > -1) { - cueInstanceCounts[name] -= 1; activeCues[lowestIndex].Stop(AudioStopOptions.AsAuthored); return true; } @@ -332,12 +312,21 @@ namespace Microsoft.Xna.Framework.Audio if (activeCues.Contains(cue)) { activeCues.Remove(cue); - cueInstanceCounts[cue.Name] -= 1; + cueInstanceCounts[cue.Name].Remove(cue); } } } } + internal int INTERNAL_cueInstanceCount(string name) + { + if (!cueInstanceCounts.ContainsKey(name)) + { + cueInstanceCounts.Add(name, new List()); + } + return cueInstanceCounts[name].Count; + } + #endregion } } diff --git a/src/Audio/Cue.cs b/src/Audio/Cue.cs index e19d0e5..33226b1 100644 --- a/src/Audio/Cue.cs +++ b/src/Audio/Cue.cs @@ -88,12 +88,12 @@ namespace Microsoft.Xna.Framework.Audio #region Internal Properties + private ulong elapsedFrames; internal bool JustStarted { get { - // Arbitrarily 1/12 of a second, with some wiggle room -flibit - return INTERNAL_timer.ElapsedMilliseconds < 80; + return elapsedFrames < 2; } } @@ -306,6 +306,10 @@ namespace Microsoft.Xna.Framework.Audio return curVar.GetValue(); } } + if (name.Equals("NumCueInstances")) + { + return INTERNAL_category.INTERNAL_cueInstanceCount(Name); + } throw new ArgumentException("Instance variable not found!"); } @@ -328,9 +332,7 @@ namespace Microsoft.Xna.Framework.Audio throw new InvalidOperationException("Cue already playing!"); } - INTERNAL_category.INTERNAL_initCue(this); - - if (GetVariable("NumCueInstances") >= INTERNAL_data.InstanceLimit) + if (INTERNAL_category.INTERNAL_cueInstanceCount(Name) >= INTERNAL_data.InstanceLimit) { if (INTERNAL_data.MaxCueBehavior == MaxInstanceBehavior.Fail) { @@ -369,6 +371,7 @@ namespace Microsoft.Xna.Framework.Audio return; } + elapsedFrames = 0; INTERNAL_timer.Start(); if (INTERNAL_data.FadeInMS > 0) { @@ -461,6 +464,7 @@ namespace Microsoft.Xna.Framework.Audio { return true; } + elapsedFrames += 1; // Play events when the timestamp has been hit. for (int i = 0; i < INTERNAL_eventList.Count; i += 1)