-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.cs
156 lines (130 loc) · 4.8 KB
/
Utility.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SVOscillator
{
class Utility
{
public static float ReadFloat(string msg)
{
Console.WriteLine(msg);
string input = Console.ReadLine();
float retVal;
if (!float.TryParse(input, out retVal))
{
throw new InvalidOperationException("Could not read float");
}
return retVal;
}
public static int ReadInt(string msg)
{
return (int)ReadFloat(msg);
}
public static string ReadString(string msg)
{
Console.WriteLine(msg);
return Console.ReadLine();
}
public static void WaitForUserInput(string msg)
{
Console.WriteLine(msg);
Console.ReadLine();
}
public static int BeatsPerMeasure(OsuParsers.Enums.Beatmaps.TimeSignature timeSignature)
{
int retVal = timeSignature == OsuParsers.Enums.Beatmaps.TimeSignature.SimpleQuadruple ? 4 : 3;
return retVal;
}
public static OsuParsers.Beatmaps.Objects.TimingPoint MakeGreenLine(float offset, float sv, OsuParsers.Enums.Beatmaps.TimeSignature timeSignature = OsuParsers.Enums.Beatmaps.TimeSignature.SimpleQuadruple)
{
var retVal = new OsuParsers.Beatmaps.Objects.TimingPoint();
retVal.Inherited = true;
retVal.CustomSampleSet = 1;
retVal.SampleSet = OsuParsers.Enums.Beatmaps.SampleSet.Soft;
retVal.Offset = (int)offset;
retVal.Effects = OsuParsers.Enums.Beatmaps.Effects.None;
retVal.TimeSignature = timeSignature;
retVal.Volume = 50;
retVal.BeatLength = CalcBeatlength(sv);
return retVal;
}
public static OsuParsers.Beatmaps.Objects.TimingPoint MakeRedLine(float offset, float bpm, OsuParsers.Enums.Beatmaps.TimeSignature timeSignature = OsuParsers.Enums.Beatmaps.TimeSignature.SimpleQuadruple)
{
var retVal = new OsuParsers.Beatmaps.Objects.TimingPoint();
retVal.Inherited = false;
retVal.CustomSampleSet = 1;
retVal.SampleSet = OsuParsers.Enums.Beatmaps.SampleSet.Soft;
retVal.Offset = (int)offset;
retVal.Effects = OsuParsers.Enums.Beatmaps.Effects.None;
retVal.TimeSignature = timeSignature;
retVal.Volume = 50;
retVal.BeatLength = BpmToBeatlength(bpm);
return retVal;
}
public static bool TryFindExistingByOffset(List<OsuParsers.Beatmaps.Objects.TimingPoint> timingPoints, float offset, out int result)
{
for (int i = 0; i < timingPoints.Count; i++)
{
if (timingPoints[i].Offset == (int)offset)
{
result = i;
return true;
}
}
result = -1;
return false;
}
public static bool TryFindExistingByOffset(List<OsuParsers.Beatmaps.Objects.TimingPoint> timingPoints, float offset, out List<int> result)
{
result = new List<int>();
bool retVal = false;
for (int i = 0; i < timingPoints.Count; i++)
{
if (timingPoints[i].Offset == (int)offset)
{
result.Add(i);
retVal = true;
}
}
return retVal;
}
public static bool TryFindMostRecentTimingPoint(List<OsuParsers.Beatmaps.Objects.TimingPoint> timingPoints, float offset, out int result, bool inherited = true)
{
for (float time = (int)offset; time >= 0; time--)
{
if (TryFindExistingByOffset(timingPoints, time, out List<int> candidates))
{
foreach (int idx in candidates)
{
if (timingPoints[idx].Inherited == inherited)
{
result = idx;
return true;
}
}
continue;
}
}
result = -1;
return false;
}
public static float CalcBeatlength(float sv)
{
return 1 / ((float)-0.01 * sv);
}
public static float CalcSv(double beatLength)
{
return -1f / (0.01f * (float)beatLength);
}
public static double BpmToBeatlength(float bpm)
{
return 60000f / bpm;
}
public static float BeatlengthToBpm(double beatLength)
{
return 60000f / (float)beatLength;
}
}
}