/
Extensions.List.cs
207 lines (177 loc) · 6.85 KB
/
Extensions.List.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
/*
* Copyright(c) 2021 MoogleTroupe
* Licensed under the GPL v3 license. See https://github.com/BardMusicPlayer/BardMusicPlayer/blob/develop/LICENSE for full license information.
*/
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BardMusicPlayer.DryWetMidi.Common.DataTypes;
using BardMusicPlayer.DryWetMidi.Interaction.Notes;
namespace BardMusicPlayer.Transmogrify.Processor.Utilities;
internal static partial class Extensions
{
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static Task<List<Note>> FixChords(this List<Note> notes)
{
notes = notes.OrderBy(x=>x.Time).Reverse().ToList();
for (var i = 1; i < notes.Count; i++)
{
var time = notes[i].Time;
var lowestParent = notes[0].Time;
for (var k = i - 1; k >= 0; k--)
{
var lastOn = notes[k].Time;
if (lastOn < lowestParent) lowestParent = lastOn;
}
if (lowestParent > time + 50) continue;
time = lowestParent - 50;
if (time < 0) continue;
notes[i].Time = time;
notes[i].Length = 25;
}
return Task.FromResult(notes.OrderBy(x => x.Time).ToList());
}
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static async Task<List<Note>> FixChords(this Task<List<Note>> notes) => await FixChords(await notes);
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static Task<List<Note>> OffSet50Ms(this List<Note> notes)
{
notes = notes.OrderBy(x=>x.Time).ToList();
var thisNotes = new List<Note>(notes.Count);
long lastStartTime = 0;
long lastStopTime = 0;
long lastDur = 0;
var lastNoteNum = 0;
long last50MsTimeStamp = 0;
var hasNotes = false;
var lastChannel = 0;
var lastVelocity = 0;
for (var j = 0; j < notes.Count; j++)
{
var startTime = notes[j].GetTimedNoteOnEvent().Time;
long stopTime;
var dur = notes[j].Length;
int noteNum = notes[j].NoteNumber;
int channel = notes[j].Channel;
int velocity = notes[j].Velocity;
hasNotes = true;
if (j == 0)
{
if (startTime % 100 != 0 && startTime % 100 != 50)
{
if (startTime % 100 < 25 || startTime % 100 < 75 && startTime % 100 > 50) startTime = 50 * ((startTime + 49) / 50);
else startTime = 50 * (startTime / 50);
}
if (dur % 100 != 0 && dur % 100 != 25 && dur % 100 != 50 && dur % 100 != 75)
{
dur = 25 * ((dur + 24) / 25);
if (dur < 25) dur = 25;
}
stopTime = startTime + dur;
last50MsTimeStamp = startTime;
lastStartTime = startTime;
lastStopTime = stopTime;
lastDur = dur;
lastNoteNum = noteNum;
lastChannel = channel;
lastVelocity = velocity;
}
else
{
while (last50MsTimeStamp < startTime) last50MsTimeStamp += 50;
startTime = last50MsTimeStamp;
stopTime = startTime + dur;
if (startTime - lastStopTime < 25)
lastDur = startTime - lastStartTime - 25;
if (dur % 100 != 0 && dur % 100 != 25 && dur % 100 != 50 && dur % 100 != 75)
{
dur = 25 * ((dur + 24) / 25);
if (dur < 25) dur = 25;
}
if (lastDur > 0)
thisNotes.Add(new Note((SevenBitNumber) lastNoteNum, lastDur, lastStartTime)
{
Channel = (FourBitNumber) lastChannel,
Velocity = (SevenBitNumber) lastVelocity,
OffVelocity = (SevenBitNumber) lastVelocity
});
lastStartTime = startTime;
lastStopTime = stopTime;
lastDur = dur;
lastNoteNum = noteNum;
lastChannel = channel;
lastVelocity = velocity;
}
}
if (hasNotes)
thisNotes.Add(new Note((SevenBitNumber) lastNoteNum, lastDur, lastStartTime)
{
Channel = (FourBitNumber) lastChannel,
Velocity = (SevenBitNumber) lastVelocity,
OffVelocity = (SevenBitNumber) lastVelocity
});
return Task.FromResult(thisNotes.OrderBy(x => x.Time).ToList());
}
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static async Task<List<Note>> OffSet50Ms(this Task<List<Note>> notes) => await OffSet50Ms(await notes);
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static Task<List<Note>> FixEndSpacing(this List<Note> notes)
{
notes = notes.OrderBy(x=>x.Time).ToList();
var thisNotes = new List<Note>(notes.Count);
for (var j = 0; j < notes.Count; j++)
{
var noteNum = notes[j].NoteNumber;
var time = notes[j].Time;
var dur = notes[j].Length;
var channel = notes[j].Channel;
var velocity = notes[j].Velocity;
if (j + 1 < notes.Count)
{
if (notes[j].Length >= 100 && notes[j + 1].Time <= notes[j].Time + notes[j].Length + 60)
{
dur = notes[j + 1].Time - notes[j].Time - 60;
dur = dur < 60 ? 60 : dur;
}
else if (notes[j].Length < 100 && notes[j + 1].Time <= notes[j].Time + notes[j].Length + 25)
{
dur = notes[j + 1].Time - notes[j].Time - 25;
dur = dur < 25 ? 25 : dur;
}
}
thisNotes.Add(new Note(noteNum, dur, time)
{
Channel = channel,
Velocity = velocity,
OffVelocity = velocity
});
}
return Task.FromResult(thisNotes.OrderBy(x => x.Time).ToList());
}
/// <summary>
///
/// </summary>
/// <param name="notes"></param>
/// <returns></returns>
internal static async Task<List<Note>> FixEndSpacing(this Task<List<Note>> notes) => await FixEndSpacing(await notes);
}