This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
260 lines (226 loc) · 8.34 KB
/
MainWindow.xaml.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace ShadowBBR_Editor
{
public partial class MainWindow : Window
{
//Constants
private const int UpdateInterval = 10;
private const string PlayImageLocation = @"/icon/play.png";
private const string PauseImageLocation = @"/icon/pause.png";
private const string OpenImageLocation = @"icon/bpmOff.png";
private const string CloseImageLocation = @"icon/bpmOn.png";
private const string TimelineFormat = "{0}:{1} ({2})";
//Private variables
private DispatcherTimer dispatcherTimer;
private PlaybackSpeed playbackSpeed = PlaybackSpeed.Normal;
private double lastSliderPosition = 0.5;
private bool playbackAcive = false;
private bool bpmIconFill = false;
private bool sliderAnimated = false;
private int lastBPM = 0;
private static readonly Dictionary<PlaybackSpeed, double> playbackSpeedLookup = new Dictionary<PlaybackSpeed, double>()
{ { PlaybackSpeed.VerySlow, 0.35 }, { PlaybackSpeed.Slow, 0.7 }, { PlaybackSpeed.Normal, 1 }, { PlaybackSpeed.Fast, 1.25 }, { PlaybackSpeed.VeryFast, 1.5 }, { PlaybackSpeed.Fastest, 2 }};
//Properties
public int SongBeatLength => (int)Math.Floor(MediaPlayer.NaturalDuration.TimeSpan.TotalMinutes * BPMSlider.Value);
public string VolumeString => VolumeSlider.Value == 0 ? @"/icon/volume-muted.png" : VolumeSlider.Value > 0.75 ? @"/icon/volume-loud.png" : VolumeSlider.Value > 0.2 ? @"/icon/volume.png" : @"/icon/volume-low.png";
public int SongMaxMinutes => (int)MediaPlayer.NaturalDuration.TimeSpan.TotalMinutes;
public int SongMaxSeconds => MediaPlayer.NaturalDuration.TimeSpan.Seconds;
public int SongBeatPosition => (int)Math.Floor(MediaPlayer.Position.TotalMinutes * BPMSlider.Value);
#region Utility Events
private void ImportButton_Click(object sender, RoutedEventArgs e)
{
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
}
#endregion
#region Timeline Control Events
private void PlayButton_Click(object sender, RoutedEventArgs e)
{
SetPlayState(!playbackAcive);
}
private void RestartButton_Click(object sender, RoutedEventArgs e)
{
if (double.TryParse(RestartField.Text, out double value))
{
MediaPlayer.Position = TimeSpan.FromMilliseconds((int)Math.Ceiling(value / BPMSlider.Value * 60000));
}
else
{
MediaPlayer.Position = new TimeSpan(0);
RestartField.Text = "0";
}
}
private void VolumeButton_Click(object sender, RoutedEventArgs e)
{
SetMuteState(VolumeSlider.Value != 0);
}
private void AudioImportButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
SetPlayState(false);
MediaPlayer.Source = new Uri(openFileDialog.FileName, UriKind.Absolute);
}
}
private void PlaybackSpeedButton_Click(object sender, RoutedEventArgs e)
{
playbackSpeed = (PlaybackSpeed)(((int)playbackSpeed + 1) % Enum.GetNames(typeof(PlaybackSpeed)).Length);
SetPlaybackSpeed(playbackSpeedLookup[playbackSpeed]);
}
private void RestartField_LostFocus(object sender, RoutedEventArgs e)
{
if (double.TryParse(RestartField.Text, out double value))
{
if (MediaPlayer.NaturalDuration.HasTimeSpan)
{
double newValue = Math.Max(Math.Min(value, SongBeatLength), 0);
RestartField.Text = newValue.ToString();
}
}
else
{
RestartField.Text = "0";
}
}
private void MediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
{
TimelineSlider.Maximum = MediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds;
playbackSpeed = PlaybackSpeed.Normal;
SetPlaybackSpeed(playbackSpeedLookup[playbackSpeed]);
}
private void MediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
{
MediaPlayer.Position = new TimeSpan(0);
}
private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
MediaPlayer.Volume = VolumeSlider.Value;
VolumeIcon.Source = new BitmapImage(new Uri(VolumeString, UriKind.Relative));
}
private void TimelineSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (sliderAnimated) return;
MediaPlayer.Position = TimeSpan.FromMilliseconds((int)TimelineSlider.Value);
}
#endregion
#region Metadata Events
private void BPMSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
SetMetadataBPM(Math.Round(BPMSlider.Value, Mouse.LeftButton == MouseButtonState.Pressed ? 0 : 1));
}
private void BPMField_LostFocus(object sender, RoutedEventArgs e)
{
if (double.TryParse(BPMField.Text, out double value))
{
SetMetadataBPM(value);
}
else
{
BPMField.Text = BPMSlider.Value.ToString();
}
}
#endregion
public MainWindow()
{
InitializeComponent();
MediaPlayer.Volume = VolumeSlider.Value;
dispatcherTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(UpdateInterval)
};
dispatcherTimer.Tick += new EventHandler(UpdateTimelineSlider);
dispatcherTimer.Tick += new EventHandler(UpdateMetronome);
dispatcherTimer.Start();
BeatmapTimeline timeline = new BeatmapTimeline(1145, 295);
timeline.Setup(256, 80, 4);
TimelineViewer.Children.Add(timeline);
timeline.AddBeat(12);
timeline.AddBeat(14);
timeline.AddBeat(16);
}
/// <summary>
/// Set the metadata BPM value and update UI elements to reflect the value.
/// </summary>
/// <param name="value">The BPM value to set. (Will be clamped to the BPM slider range)</param>
private void SetMetadataBPM(double value)
{
double newValue = Math.Max(Math.Min(value, BPMSlider.Maximum), BPMSlider.Minimum);
BPMSlider.Value = newValue;
BPMField.Text = newValue.ToString();
}
/// <summary>
/// Updates the timeline values to reflect the current playback state.
/// </summary>
private void UpdateTimelineSlider(object sender, EventArgs e)
{
sliderAnimated = true;
TimelineSlider.Value = MediaPlayer.Position.TotalMilliseconds;
if (MediaPlayer.NaturalDuration.HasTimeSpan)
{
TimelineObjectiveLabel.Text = string.Format(TimelineFormat, SongMaxMinutes, SongMaxSeconds.ToString("D2"), SongBeatLength);
TimelineCurrentLabel.Text = string.Format(TimelineFormat, (int)MediaPlayer.Position.TotalMinutes, MediaPlayer.Position.Seconds.ToString("D2"), SongBeatPosition);
}
sliderAnimated = false;
}
/// <summary>
/// Checks if the current beat has changed, if it has toggles the metronome icon. Hides icon if playback has stopped.
/// </summary>
private void UpdateMetronome(object sender, EventArgs e)
{
BPMIcon.Visibility = playbackAcive ? Visibility.Visible : Visibility.Hidden;
if (lastBPM != SongBeatPosition && Mouse.LeftButton == MouseButtonState.Released)
{
lastBPM = SongBeatPosition;
bpmIconFill = !bpmIconFill;
BPMIcon.Source = new BitmapImage(new Uri(bpmIconFill ? CloseImageLocation : OpenImageLocation, UriKind.Relative));
}
}
/// <summary>
/// Set the playback speed of the media player and update UI elements to reflect the speed.
/// </summary>
/// <param name="value">The playback speed to play at.</param>
private void SetPlaybackSpeed(double value)
{
MediaPlayer.SpeedRatio = value;
PlaybackSpeedText.Text = string.Format("{0}x", Math.Round(value, 1).ToString());
}
/// <summary>
/// Set the mute state of the media player and set UI elements to reflect the mute state.
/// </summary>
/// <param name="value">True for muted, false for unmuted</param>
private void SetMuteState(bool value)
{
lastSliderPosition = value ? Math.Max(VolumeSlider.Value, 0.1) : lastSliderPosition;
VolumeSlider.Value = MediaPlayer.Volume = value ? 0 : lastSliderPosition;
VolumeIcon.Source = new BitmapImage(new Uri(VolumeString, UriKind.Relative));
}
/// <summary>
/// Set the playback state of the media player and set UI elements to reflect playback.
/// </summary>
/// <param name="value">True to play, false to pause</param>
private void SetPlayState(bool value)
{
if (value) MediaPlayer.Play();
else MediaPlayer.Pause();
PlayIcon.Source = new BitmapImage(new Uri(value ? PauseImageLocation : PlayImageLocation, UriKind.Relative));
playbackAcive = value;
}
}
}
public enum PlaybackSpeed
{
VerySlow,
Slow,
Normal,
Fast,
VeryFast,
Fastest
}