-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.py
322 lines (268 loc) · 8.8 KB
/
scratch.py
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import json
import mido
def groupNotesByTimes(time,notes):
foundNotes = []
for note in notes:
if abs(time - note['t']) <= 1:
foundNotes.append(note)
return foundNotes
def get_tempo(time, tempo_times):
if not tempo_times:
return None # Handle empty tempo_times list
# Check first tempo without decrementing i
if time <= tempo_times[0]['t']:
return tempo_times[0]
for i, tempo_change in enumerate(tempo_times[1:], start=1):
check = i - 1
if tempo_times[check]['t'] <= time and tempo_change['t'] >= time:
return tempo_times[check]
# Return last tempo if no match is found
return tempo_times[-1]
# length of a beat: 535ms
# bpm=112
def altCode():
trackInfos = [
{
"midi-track-name": "PLASTIC DRUMS",
"track-variation-suffix": "-drums",
"track-difficulty-prefix": "pro-",
"istap": False
},
{
"midi-track-name": "PLASTIC GUITAR",
"track-variation-suffix": "-guitar",
"track-difficulty-prefix": "pro-",
"istap": False
},
{
"midi-track-name": "PLASTIC BASS",
"track-variation-suffix": "-bass",
"track-difficulty-prefix": "pro-",
"istap": False
},
{
"midi-track-name": "PART VOCALS",
"track-variation-suffix": "",
"track-difficulty-prefix": "",
"istap": True
},
{
"midi-track-name": "PART GUITAR",
"track-variation-suffix": "-guitar",
"track-difficulty-prefix": "",
"istap": True
},
{
"midi-track-name": "PART DRUMS",
"track-variation-suffix": "-drums",
"track-difficulty-prefix": "",
"istap": True
},
{
"midi-track-name": "PART BASS",
"track-variation-suffix": "-bass",
"track-difficulty-prefix": "",
"istap": True
},
{
"midi-track-name": "SECTION",
"track-variation-suffix": "-section",
"track-difficulty-prefix": "",
"istap": True
},
{
"midi-track-name": "BEAT",
"track-variation-suffix": "-beat",
"track-difficulty-prefix": "",
"istap": True,
"track_is_special": True
},
{
"midi-track-name": "EVENTS",
"track-variation-suffix": "-events",
"track-difficulty-prefix": "",
"istap": True
}
]
bpm = 76.65
beatCrochetMs = (60 / bpm) * 1000
noteLengthInSteps = 12
stepCrochetMs = beatCrochetMs / noteLengthInSteps
ticksPerBeat = 480
midi = mido.MidiFile()
midi.ticks_per_beat = ticksPerBeat
tempoTrack = mido.MidiTrack()
tempoTrack.append(
mido.MetaMessage('set_tempo', time=0, tempo=mido.bpm2tempo(bpm))
)
midi.tracks.append(tempoTrack)
for track in trackInfos:
trackSuffixFile = track['track-variation-suffix']
trackDiffPrefix = track['track-difficulty-prefix']
trackMidiName = track['midi-track-name']
isTrackTap = track['istap']
trackIsSpecial = track.get('track_is_special', False)
midiTrack = mido.MidiTrack()
midiTrack.name = trackMidiName
#trackMeta = json.loads(open('_fnfc/partypacker-metadata-guitar.json', 'r').read())
trackNotes = json.loads(open(f'aishite/aishite-chart{trackSuffixFile}.json', 'r').read())
trackNotes_easy = trackNotes['notes'].get(f'{trackDiffPrefix}easy', [])
trackNotes_medi = trackNotes['notes'].get(f'{trackDiffPrefix}medium', [])
trackNotes_hard = trackNotes['notes'].get(f'{trackDiffPrefix}hard', [])
trackNotes_expe = trackNotes['notes'].get(f'{trackDiffPrefix}expert', [])
trackEvents = trackNotes['events']
timedNotes = processTimes(
[trackNotes_easy,
trackNotes_medi,
trackNotes_hard,
trackNotes_expe],
isTrackTap,
stepCrochetMs,
trackEvents,
trackIsSpecial,
trackMidiName
)
lastEventTime = 0
for event in timedNotes:
eventTime = event['time'] - lastEventTime
beatsSince = 0
try:
beatsSince = eventTime / beatCrochetMs
except:
print('Some unknown error')
#print(f'{eventTime = } \n{event['data'] = } \n{event['event'] = }\n{beatsSince = }')
ticksSince = round(beatsSince * ticksPerBeat)
if ticksSince < 0:
print(f'Fuck you i found a dumbass thats negative {ticksSince}')
return
message = None
if event['event'].startswith('note'):
message = mido.Message(event['event'], time=ticksSince, velocity=96, note=event['data'])
if event['event'] == 'text':
message = mido.MetaMessage(event['event'], time=ticksSince, text=event['data'])
if message:
midiTrack.append(message)
else: # Avoid setting the next event time because we couldnt add this event to midi
continue
lastEventTime = event['time']
midi.tracks.append(midiTrack)
midi.save('testmidi.mid')
def processTimes(tracks = [], isTap = True, defaultNoteDuration = 100, events = [], trackIsSpecial = False, trackName = 'NOT SET'):
notesSortedAsEvents = []
liftThreshold = 6
midiKeys_Tap = [ # Non-plastic
{ # Track 0, EASY
0: 60,
1: 61,
2: 62,
3: 63
},
{ # Track 1, MEDIUM
0: 72,
1: 73,
2: 74,
3: 75
},
{ # Track 2, HARD
0: 84,
1: 85,
2: 86,
3: 87
},
{ # Track 3, EXPERT
7: 96,
0: 97,
1: 98,
2: 99,
3: 100,
4: 116 # Overdrive
}
]
midiKeys_Plastic = [ # plastic track
{ # Track 0, EASY
7: 60,
0: 61,
1: 62,
2: 63,
3: 64
},
{ # Track 1, MEDIUM
7: 72,
0: 73,
1: 74,
2: 75
},
{ # Track 2, HARD
7: 84,
0: 85,
1: 86,
2: 87,
3: 88
},
{ # Track 3, EXPERT
7: 96,
0: 97,
1: 98,
2: 99,
3: 100,
4: 116 # Overdrive
}
]
midiKeysTemplate = midiKeys_Tap if isTap else midiKeys_Plastic
if trackIsSpecial:
midiKeysTemplate = [ # BEAT track notes
{
4: 12,
0: 13
},
{}, {}, {}
]
for i, track in enumerate(tracks):
for j, note in enumerate(track):
strumTime = note['t']
noteData = note['d']
susLength = note['l']
noteKey = midiKeysTemplate[i].get(noteData, 0)
if noteKey == 0:
print(f'Could not find note for {noteData} in difficulty {i} of track {trackName}, ignoring')
continue
if susLength <= 0:
# Apply a default length, since midi needs it
susLength = defaultNoteDuration
notesSortedAsEvents.append({
"time": strumTime,
"data": noteKey,
"event": "note_on"
})
notesSortedAsEvents.append({
"time": strumTime + susLength,
"data": noteKey,
"event": "note_off"
})
if note.get('k', '') == 'Lift':
notesSortedAsEvents.append({
"time": strumTime,
"data": noteKey + liftThreshold,
"event": "note_on"
})
notesSortedAsEvents.append({
"time": strumTime + susLength,
"data": noteKey + liftThreshold,
"event": "note_off"
})
for event in events:
eventTime = event['t']
eventType = event['v']['type']
eventText = event['v']['text']
eventIsTapEvent = not event['v']['plastic_track']
# Check the boolean plastic track
if isTap == eventIsTapEvent:
if eventType != 'text':
eventText = eventType
notesSortedAsEvents.append({
"time": eventTime,
"data": eventText,
"event": "text"
})
return sorted(notesSortedAsEvents, key=lambda note: note['time'])
altCode()