-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundFader.cs
89 lines (77 loc) · 2.23 KB
/
SoundFader.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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace SpaceBaseMono
{
class SoundFader : GameComponent
{
enum State { FADING_IN, PLAY, FADING_OUT, STOP };
State state;
private SoundEffectInstance sound;
private Game game;
private float volume;
private float maxvol;
public SoundFader(Game game) :base (game)
{
this.game = game;
}
public SoundFader(Game game, SoundEffectInstance sound, float maxvol=1.0f)
: base(game)
{
this.game = game;
this.sound = sound;
this.maxvol = maxvol;
}
public void FadeOut()
{
state = State.FADING_OUT;
}
public void Play()
{
state = State.PLAY;
sound.Play();
volume = maxvol;
}
public void Stop()
{
state = State.STOP;
sound.Stop();
volume = maxvol;
}
public void FadeIn()
{
volume = 0.0f;
state = State.FADING_IN;
sound.Play();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
switch (state)
{
case State.FADING_IN:
volume += 0.005f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (volume >= maxvol)
{
state = State.PLAY;
volume = maxvol;
}
break;
case State.FADING_OUT:
volume -= 0.0005f * (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (volume <= 0)
{
Stop();
}
break;
}
sound.Volume = Easing.EaseOut(volume, EasingType.Quadratic);
}
}
}