-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightMIDI.cs
172 lines (139 loc) · 6.73 KB
/
LightMIDI.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
using System;
using System.Collections.Generic;
using System.Linq;
using BrewLib.Util;
using StorybrewCommon.Storyboarding;
using StorybrewCommon.Storyboarding.Util;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;
namespace StorybrewScripts
{
class LightMIDI : StorybrewCommon.Scripting.StoryboardObjectGenerator
{
const int keyCount = 52;
const float pWidth = 700, keySpacing = pWidth / keyCount;
[Configurable] public string MIDIPath = "";
protected override void Generate()
{
#region Initialize Constants
var layer = GetLayer("");
var keyRect = BitmapHelper.FindTransparencyBounds(GetMapsetBitmap(getKeyFile("00")));
var pScale = MathF.Round(keySpacing / (keyRect.Width - keyCount / 9f), 3);
#endregion
#region Draw Piano
char[] keyNames = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
List<OsbSprite> keys = [];
Dictionary<string, float> keyPositions = [];
Dictionary<string, (OsbSprite, OsbSprite)> keyHighlights = [];
Dictionary<char, string> keyFiles = new()
{
{'C', "10"},
{'D', "00"},
{'E', "01"},
{'F', "10"},
{'G', "00"},
{'A', "00"},
{'B', "01"}
};
for (int i = 0, keyOctave = 0; i < keyCount; ++i)
{
var keyNameIndex = i % 7 - 2;
if (keyNameIndex == 0) ++keyOctave;
else if (keyNameIndex < 0) keyNameIndex = keyNames.Length + keyNameIndex;
var keyName = keyNames[keyNameIndex];
var keyType = keyFiles[keyName];
var keyFile = getKeyFile(keyType);
unsafe
{
fixed (char* pChars = keyFile)
if (i == 0) pChars[keyFile.Length - 6] = '1';
else if (i == keyCount - 1) pChars[keyFile.Length - 5] = '1';
}
var pX = MathF.Round(320 - pWidth / 2 + i * keySpacing + keyRect.Width * pScale / 2, 1);
var p = layer.CreateSprite(keyFile, OsbOrigin.TopCentre, new(pX, 240));
p.Scale(-1843, pScale);
var hl = layer.CreateSprite(getKeyFile(keyType, true), OsbOrigin.TopCentre, new(pX, 240));
hl.Scale(25, pScale);
var sp = layer.CreateSprite("sb/l.png", OsbOrigin.BottomCentre, new(pX, 240));
sp.ScaleVec(25, MathF.Round(pScale * 2.5f, 2), pScale);
var keyFullName = $"{keyName}{keyOctave}";
keyHighlights[keyFullName] = (hl, sp);
keyPositions[keyFullName] = pX;
keys.Add(p);
if (keyFile[^5] == '0')
{
pX += MathF.Round(keySpacing / 2, 1);
p = layer.CreateSprite("sb/k/bb.png", OsbOrigin.TopCentre, new(pX, 240));
p.Scale(-1843, pScale);
hl = layer.CreateSprite("sb/k/bbl.png", OsbOrigin.TopCentre, new(pX, 240));
hl.Scale(25, pScale);
sp = layer.CreateSprite("sb/l.png", OsbOrigin.BottomCentre, new(pX, 240));
sp.ScaleVec(25, MathF.Round(pScale * 1.25f, 2), pScale);
keyFullName = $"{keyName}Sharp{keyOctave}";
keyHighlights[keyFullName] = (hl, sp);
keyPositions[keyFullName] = pX;
keys.Add(p);
}
}
var delay = (float)Beatmap.TimingPoints.First().BeatDuration * 2 / keys.Count;
var keyTime = 0f;
foreach (var key in keys)
{
var introStartTime = -1843 + keyTime;
var introEndTime = introStartTime + 100;
var outroStartTime = 171756 + keyTime;
var outroEndTime = outroStartTime + 100;
key.Fade(introStartTime, introEndTime, 0, 1);
key.Fade(outroStartTime, outroEndTime, 1, 0);
keyTime += delay;
}
#endregion
CreateNotes(keyPositions, keyHighlights, layer);
}
void CreateNotes(Dictionary<string, float> positions, Dictionary<string, (OsbSprite, OsbSprite)> highlights, StoryboardLayer layer)
{
const int scrollTime = 2300;
AddDependency(AssetPath + "/" + MIDIPath);
var file = MidiFile.Read(AssetPath + "/" + MIDIPath);
var map = file.GetTempoMap();
var trackIndex = 0;
foreach (var track in file.Chunks.OfType<TrackChunk>())
{
var notes = track.Events.GetNotes();
if (notes.Count == 0) continue;
using OsbSpritePool pool = new(layer, "sb/p.png", OsbOrigin.BottomCentre, (p, s, e) =>
{
p.Additive(s);
if (trackIndex == 1) p.Color(s, 28f / 51, 35f / 51, 13f / 17);
else p.Color(s, 4f / 17, 4f / 17, 2f / 3);
});
foreach (var note in notes)
{
var time = TimeConverter.ConvertTo<MetricTimeSpan>(note.Time, map).TotalMilliseconds + 25;
var endTime = TimeConverter.ConvertTo<MetricTimeSpan>(note.EndTime, map).TotalMilliseconds + 25;
var length = endTime - time;
if (length <= 0) continue;
var key = note.NoteName.ToString() + note.Octave;
var noteWidth = (int)(key.Contains('S') ? keySpacing / 2 : keySpacing);
var noteLength = (int)(length * 240 / scrollTime);
var n = pool.Get(time - scrollTime, endTime);
if (n.StartTime != double.MaxValue) n.ScaleVec(time - scrollTime, noteWidth, noteLength);
n.Move(time - scrollTime, time, positions[key], 0, positions[key], 240);
n.ScaleVec(time, endTime, noteWidth, noteLength, noteWidth, 0);
var splashes = highlights[key];
splashes.Item1.Fade(time, time, 0, 1);
splashes.Item2.Fade(time, time, 0, 1);
splashes.Item1.Fade(endTime, 0);
splashes.Item2.Fade(endTime, 0);
}
++trackIndex;
}
foreach (var highlight in highlights.Values) if (highlight.Item1.CommandCount < 2)
{
layer.Discard(highlight.Item1);
layer.Discard(highlight.Item2);
}
}
static string getKeyFile(string keyType, bool highlight = false) => highlight ? $"sb/k/{keyType}l.png" : $"sb/k/{keyType}.png";
}
}