-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodparser.py
More file actions
145 lines (113 loc) · 5.01 KB
/
Copy pathmodparser.py
File metadata and controls
145 lines (113 loc) · 5.01 KB
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
import json
class mod:
def __init__(self, file):
self.notesToInts = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
self.channels = []
mod_dump = file.decode()
print(mod_dump)
rows_arr = []
for i in mod_dump.split('\n'):
if i.startswith('|'):
rows_arr.append(i)
channel_count = len(rows_arr[0].split('|')) - 1
for i in range(channel_count):
self.channels.append({'name': f'channel {i}', 'instruments': {}})
# CREATE CHANNEL ARRAYS
channel_arr = [[] for _ in range(channel_count)]
for i in range(len(rows_arr)):
row_split_arr = rows_arr[i].split('|')[1:]
for channel in range(channel_count):
note = row_split_arr[channel][:3]
if note == '===':
pitch = '==='
else:
try:
pitch = self.notesToInts[note[0]]
if note[1] == '#':
pitch += 1
elif note[1] == 'b':
pitch -= 1
pitch += (int(note[2]) - 4) * 12
except:
pitch = ''
instrument = row_split_arr[channel][3:5]
channel_arr[channel].append({'note': pitch, 'instr': instrument})
# SPLIT INSTRUMENTS
for channel in channel_arr:
for n in range(len(channel)):
pitch = channel[n]['note']
instrument = channel[n]['instr']
if n == 0:
last_instrument = instrument
# print(pitch, instrument)
current_channel = channel_arr.index(channel)
# WELL THIS IS UGLY IM SRY
if (
instrument != '..'
and instrument not in self.channels[current_channel]['instruments']
):
self.channels[current_channel]['instruments'][str(instrument)] = []
self.channels[current_channel]['instruments'][
str(instrument)
].extend(list([''] * n))
if instrument == '..':
for _ in self.channels[current_channel]['instruments']:
if pitch != '===':
self.channels[current_channel]['instruments'][_].append('')
else:
self.channels[current_channel]['instruments'][_].append(
'==='
)
else:
for _ in self.channels[current_channel]['instruments']:
if instrument == _:
self.channels[current_channel]['instruments'][_].append(
pitch
)
else:
if instrument != last_instrument:
self.channels[current_channel]['instruments'][_].append(
'==='
)
else:
self.channels[current_channel]['instruments'][_].append(
''
)
last_instrument = instrument
# OK NOW FINAL PARSE I GUESS
# MY HEAD HURTS
# print(self.channels[2])
for channel in self.channels:
for instrument in channel['instruments']:
# print(channel['instruments'][instrument])
current_note = ''
last_note = 'start'
duration = 1
temp_pattern_list = []
counter = 0
for note in channel['instruments'][instrument]:
counter += 1
# print(note)
if last_note == 'start':
last_note = note
elif note == '':
duration += 1
elif note == '===':
temp_pattern_list.append(f'{last_note}/{duration/4}')
last_note = ''
duration = 1
else:
temp_pattern_list.append(f'{last_note}/{duration/4}')
last_note = note
duration = 1
if counter == len(channel['instruments'][instrument]):
temp_pattern_list.append(f'{last_note}/{duration/4}')
# print('end')
pattern = ' '.join(temp_pattern_list)
channel['instruments'][instrument] = pattern
# print(self.channels)
file = open('./temp/mod_to_bot.json', 'w')
jsonfile = json.dumps(self.channels, sort_keys=True, indent=4)
file.write(jsonfile)
file.close()
print('ok! check mod_to_bot.json file')