forked from Rampastring/Rampastring.XNAUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnhancedSoundEffect.cs
162 lines (135 loc) · 4.72 KB
/
EnhancedSoundEffect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
namespace Rampastring.XNAUI;
/// <summary>
/// A toggleable sound effect that can also have a defined priority
/// and a decay rate for the priority.
/// </summary>
public class EnhancedSoundEffect : IDisposable
{
/// <summary>
/// Creates a new prioritized sound. Loads the specified sound asset.
/// </summary>
/// <param name="assetName">The asset name of the sound file to load.</param>
public EnhancedSoundEffect(string assetName)
{
soundEffect = AssetLoader.LoadSound(assetName);
}
/// <summary>
/// Creates a new enhanced sound. Uses the given sound assert.
/// </summary>
/// <param name="soundEffect">The sound effect.</param>
public EnhancedSoundEffect(SoundEffect soundEffect)
{
this.soundEffect = soundEffect;
}
/// <summary>
/// Creates a new enhanced sound. Loads the specified sound asset.
/// </summary>
/// <param name="assetName">The asset name of the sound file to load.</param>
/// <param name="priority">The priority of this sound</param>
/// <param name="priorityDecayRate">The priority decay rate of this sound.</param>
/// <param name="repeatPrevention">If set above zero, will prevent the sound from being played again
/// for the specified number of seconds after it has been played.</param>
public EnhancedSoundEffect(string assetName, double priority, double priorityDecayRate, float repeatPrevention) : this(assetName)
{
Priority = priority;
PriorityDecayRate = priorityDecayRate;
RepeatPrevention = repeatPrevention;
}
private SoundEffect soundEffect;
private DateTime lastPlayTime;
/// <summary>
/// Gets or sets a bool that determines whether this sound is enabled.
/// </summary>
public bool Enabled { get; set; } = true;
/// <summary>
/// The priority of this sound.
/// While this sound is playing, sounds with less priority than this sound's
/// priority will not be played.
/// </summary>
public double Priority { get; set; }
/// <summary>
/// The decay rate of this sound's priority.
/// The sound's priority will be reduced by the specified number each second.
/// </summary>
public double PriorityDecayRate { get; set; }
/// <summary>
/// If set above zero, will prevent the sound from being played again
/// for the specified number of seconds after it has been played.
/// </summary>
public float RepeatPrevention { get; set; }
/// <summary>
/// The volume multiplier for this sound effect.
/// </summary>
public float Volume { get; set; } = 1.0f;
/// <summary>
/// Plays this sound if it's enabled.
/// </summary>
public void Play()
{
if (!Enabled || soundEffect == null)
return;
if (RepeatPrevention > 0f)
{
var dtn = DateTime.Now;
if ((dtn - lastPlayTime).TotalSeconds < RepeatPrevention)
return;
lastPlayTime = dtn;
}
SoundPlayer.Play(this);
}
internal SoundEffectInstance CreateSoundInstance()
{
if (soundEffect == null)
return null;
return soundEffect.CreateInstance();
}
/// <summary>
/// Disposes the sound effect.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
soundEffect?.Dispose();
soundEffect = null;
}
}
}
internal sealed class PrioritizedSoundInstance : IDisposable
{
public PrioritizedSoundInstance(SoundEffectInstance soundInstance,
double priority, double priorityDecayRate)
{
SoundInstance = soundInstance;
Priority = priority;
PriorityDecayRate = priorityDecayRate;
}
public SoundEffectInstance SoundInstance { get; private set; }
public double Priority { get; private set; }
public double PriorityDecayRate { get; private set; }
/// <summary>
/// Updates the priority of the sound. Returns true if the sound effect is
/// still playing, otherwise false.
/// </summary>
/// <param name="gameTime">Tells how much time has passed since the previous frame.</param>
/// <returns>True if the sound effect is still playing, otherwise false.</returns>
public bool Update(GameTime gameTime)
{
Priority = Priority - PriorityDecayRate * gameTime.ElapsedGameTime.TotalSeconds;
if (SoundInstance.State != SoundState.Playing)
return false;
return true;
}
public void Dispose()
{
SoundInstance.Dispose();
}
}