forked from joyrider3774/PlayniteSound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicFader.cs
159 lines (140 loc) · 4.94 KB
/
MusicFader.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
using System;
using System.Timers;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Playnite.SDK;
using PlayniteSounds.Models;
namespace PlayniteSounds
{
public class MusicFader
{
private static readonly ILogger Logger = LogManager.GetLogger();
private readonly MediaPlayer player;
private readonly PlayniteSoundsSettings settings;
private readonly Timer fadeTimer;
private bool isFadingOut;
private Action pauseAction;
private Action stopAction;
private Action playAction;
private bool isPaused = false;
DateTime lastCall;
DateTime lastTickCall = default;
public MusicFader(MediaPlayer player, PlayniteSoundsSettings settings)
{
this.player = player;
this.settings = settings;
fadeTimer = new Timer(50)
{
AutoReset = false
};
fadeTimer.Elapsed += (sender,e) => Application.Current?.Dispatcher?.Invoke(() => TimerTick());
}
public void Destroy()
{
fadeTimer.Close();
fadeTimer.Dispose();
}
void TimerTick()
{
double musicVolume = settings.MusicVolume / 100.0;
double fadeFrequency = 20; // 10 steps per second
if (lastTickCall != default)
{
double lastInterval = (DateTime.Now - lastTickCall).TotalMilliseconds;
if (lastInterval != 0)
{
fadeFrequency = 1000 / lastInterval;
}
}
double fadeStep = musicVolume / (settings.FadeDuration * fadeFrequency);
if (isFadingOut && player?.Clock?.CurrentState == ClockState.Active)
{
player.Volume = (player.Volume > fadeStep) ? player.Volume - fadeStep : 0;
//Logger.Trace($"Fading Out: Volume: {player.Volume}");
}
else if (isFadingOut)
{
player.Volume = 0;
//Logger.Trace($"Fading Out: Not playing");
}
else
{
player.Volume = (player.Volume + fadeStep < musicVolume ) ? player.Volume + fadeStep : musicVolume;
//Logger.Trace($"Fading In: Volume: {player.Volume}");
}
if (player.Volume >= musicVolume)
{
Logger.Trace($"Fade In: Complete in {(DateTime.Now - lastCall).TotalMilliseconds} ms");
return;
}
else if (player.Volume == 0 && pauseAction == null && playAction != null)
{
stopAction?.Invoke();
playAction.Invoke();
stopAction = playAction = null;
isFadingOut = false;
player.Volume = fadeStep;
Logger.Trace($"Fade Out: done, in {(DateTime.Now - lastCall).TotalMilliseconds} ms, do fade In");
}
else if (player.Volume == 0 && (pauseAction != null || stopAction != null))
{
pauseAction?.Invoke();
stopAction?.Invoke();
isPaused = pauseAction != null;
stopAction = pauseAction = null;
Logger.Trace($"Fade Out: Complete in {(DateTime.Now - lastCall).TotalMilliseconds} ms");
return;
}
fadeTimer.Start();
lastTickCall = DateTime.Now;
}
private void EnsureTimer()
{
lastCall = DateTime.Now;
lastTickCall = default;
fadeTimer.Start();
}
public void Pause()
{
isFadingOut = true;
pauseAction = player.Clock.Controller.Pause;
EnsureTimer();
}
public void Switch(Action stopAction, Action playAction)
{
isFadingOut = true;
this.playAction = playAction;
this.stopAction = stopAction;
EnsureTimer();
}
public void Resume()
{
if (!isPaused)
{
pauseAction = null;
if (playAction == null && stopAction == null)
{
isFadingOut = false;
}
}
else
{
if (playAction != null || stopAction != null)
{
stopAction?.Invoke();
playAction?.Invoke();
player.Volume = 0;
stopAction = playAction = null;
}
else
{
player.Clock.Controller.Resume();
}
isPaused = false;
isFadingOut = false;
}
EnsureTimer();
}
}
}