/
AudioManager.cs
120 lines (99 loc) · 3.33 KB
/
AudioManager.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
namespace Template;
public partial class AudioManager : Node
{
[Export] OptionsManager optionsManager;
GAudioPlayer musicPlayer;
Node sfxPlayersParent;
float lastPitch;
ResourceOptions options;
public override void _Ready()
{
Global.Services.Add(this, persistent: true);
options = optionsManager.Options;
musicPlayer = new GAudioPlayer(this);
sfxPlayersParent = new Node();
AddChild(sfxPlayersParent);
PlayMusic(Music.Menu);
}
public void PlayMusic(AudioStream song, bool instant = true, double fadeOut = 1.5, double fadeIn = 0.5)
{
if (!instant && musicPlayer.Playing)
{
// Transition from current song being played to new song
var tween = new GTween(musicPlayer.StreamPlayer);
// Fade out current song
tween.Animate("volume_db", -80, fadeOut)
.SetTrans(Tween.TransitionType.Sine)
.SetEase(Tween.EaseType.In);
// Set to new song
tween.Callback(() =>
{
musicPlayer.Stream = song;
musicPlayer.Play();
});
// Fade in to current song
float volume = options.MusicVolume;
float volumeRemapped =
volume == 0 ? -80 : volume.Remap(0, 100, -40, 0);
tween.Animate("volume_db", volumeRemapped, fadeIn)
.SetTrans(Tween.TransitionType.Sine)
.SetEase(Tween.EaseType.In);
}
else
{
// Instantly switch to and play new song
musicPlayer.Stream = song;
musicPlayer.Volume = options.MusicVolume;
musicPlayer.Play();
}
}
public void PlaySFX(AudioStream sound)
{
// Setup the SFX stream player
var sfxPlayer = new GAudioPlayer(sfxPlayersParent, true)
{
Stream = sound,
Volume = options.SFXVolume
};
// Randomize the pitch
var rng = new RandomNumberGenerator();
rng.Randomize();
float pitch = rng.RandfRange(0.8f, 1.2f);
// Ensure the current pitch is not the same as the last
while (Mathf.Abs(pitch - lastPitch) < 0.1f)
{
rng.Randomize();
pitch = rng.RandfRange(0.8f, 1.2f);
}
lastPitch = pitch;
// Play the sound
sfxPlayer.Pitch = pitch;
sfxPlayer.Play();
}
/// <summary>
/// Gradually fade out all sounds
/// </summary>
public void FadeOutSFX(double fadeTime = 1)
{
foreach (AudioStreamPlayer audioPlayer in sfxPlayersParent.GetChildren())
{
var tween = new GTween(audioPlayer);
tween.Animate("volume_db", -80, fadeTime);
}
}
public void SetMusicVolume(float v)
{
musicPlayer.Volume = v;
options.MusicVolume = musicPlayer.Volume;
}
public void SetSFXVolume(float v)
{
// Set volume for future SFX players
options.SFXVolume = v;
// Can't cast to GAudioPlayer so will have to remap manually again
v = v == 0 ? -80 : v.Remap(0, 100, -40, 0);
// Set volume of all SFX players currently in the scene
foreach (AudioStreamPlayer audioPlayer in sfxPlayersParent.GetChildren())
audioPlayer.VolumeDb = v;
}
}