-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgs2c.py
160 lines (134 loc) · 4.45 KB
/
gs2c.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Convert a music .asm file generated by MIDI2GSC so it does not depend on pkms.asm.
"""
from __future__ import print_function
import sys
import os.path
def parseint(s):
if s.startswith('$'):
return int(s[1:], 16)
elif s.startswith('%'):
return int(s[1:], 2)
return int(s)
def hexint(n):
return '$' + hex(n)[2:].upper()
def convert(filename):
lines = []
with open(filename, 'r') as f:
for line in f:
line = line.strip()
line = convert_line(line)
if not(line.startswith(';') or line.endswith(':') or not line):
line = '\t\t' + line
lines.append(line)
return '\n'.join(lines)
def convert_line(line):
line = line.replace(',', ', ').replace('|', ' | ').replace(' ', ' ')
# Example: db oct0 == octave 1
# Example: db oct5 == octave 6
if line.startswith('db oct'):
octave = parseint(line[6:]) + 1
return 'octave %s' % (octave,)
# Example: db (ntA# | 0) == note A#, 1
# Example: db (ntRst | 3) == note __, 4
if line.startswith('db (nt'):
_, note, _, length = line.split(None, 3)
note = note.lstrip('(')[2:]
length = length.rstrip(')')
if note == 'Rst':
note = '__'
elif not note.endswith('#'):
note += '_'
length = parseint(length) + 1
return 'note %s, %s' % (note, length)
# Example: pkmsSetVel 1, 3 == intensity $13
if line.startswith('pkmsSetVel'):
_, vel, length = line.split(None, 2)
vel = parseint(vel.rstrip(','))
length = parseint(length)
intensity = hexint((vel << 4) | length)
return 'intensity %s' % (intensity,)
# Example: pkmsSetNtr $C, 9, 4 == notetype $C, $94
if line.startswith('pkmsSetNtr'):
_, speed, vel, length = line.split(None, 3)
speed = parseint(speed.rstrip(','))
vel = parseint(vel.rstrip(','))
length = parseint(length)
intensity = hexint((vel << 4) | length)
return 'notetype %s, %s' % (speed, intensity)
# Example: pkmsSetDSpeed $C == notetype $C
if line.startswith('pkmsSetDSpeed'):
_, speed = line.split(None, 1)
return 'notetype %s' % (speed,)
# Example: pkmsEndSound == endchannel
if line.startswith('pkmsEndSound'):
return 'endchannel'
# Example: pkmsSetMod $C, 1, 2 == vibrato $C, $12
if line.startswith('pkmsSetMod'):
_, delay, depth, rate = line.split(None, 3)
delay = delay.rstrip(',')
depth = parseint(depth.rstrip(','))
rate = parseint(rate)
extent = hexint((depth << 4) | rate)
return 'vibrato %s, %s' % (delay, extent)
# Example: pkmsSetDuty 2 == dutycycle 2
if line.startswith('pkmsSetDuty'):
_, duty = line.split(None, 1)
return 'dutycycle %s' % (duty,)
# Example: pkmsSetDrums 5 == togglenoise 5
if line.startswith('pkmsSetDrums'):
_, drums = line.split(None, 1)
return 'togglenoise %s' % (drums,)
# Example: pkmsSetArp $E4 == sound_duty 0, 1, 2, 3
if line.startswith('pkmsSetArp'):
_, duty = line.split(None, 1)
duty = parseint(duty)
d1 = duty & 0b11
d2 = (duty & 0b1100) >> 2
d3 = (duty & 0b110000) >> 4
d4 = (duty & 0b11000000) >> 6
return 'sound_duty %s, %s, %s, %s' % (d1, d2, d3, d4)
# Example: pkmsSetTempo 0, $80 == tempo $80
if line.startswith('pkmsSetTempo'):
_, divider, modifier = line.split(None, 2)
divider = divider.rstrip(',')
hi = parseint(divider)
lo = parseint(modifier)
tempo = hexint((hi << 8) | lo)
return 'tempo %s' % (tempo,)
# Example: pkmsSetVolume $77 == volume $77
if line.startswith('pkmsSetVolume'):
_, volume = line.split(None, 1)
return 'volume %s' % (volume,)
# Example: pkmsCall NewSong_Channel1 + SONG_START == callchannel NewSong_Channel1
if line.startswith('pkmsCall'):
_, offset = line.split(None, 1)
offset = offset.replace('+ SONG_START', '')
return 'callchannel %s' % (offset,)
# Example: pkmsJump Channel1_Loop + SONG_START == loopchannel 0, Channel1_Loop
if line.startswith('pkmsJump'):
_, offset = line.split(None, 1)
offset = offset.replace('+ SONG_START', '')
return 'loopchannel 0, %s' % (offset,)
if line.startswith(';') or line.endswith(':') or not line:
return line
return line + ' ; WARNING: unconverted'
def write(content, old_filename):
name, ext = os.path.splitext(old_filename)
new_filename = name + '_c' + '.' + ext
with open(new_filename, 'w') as f:
f.write(content)
def main():
if len(sys.argv) < 2:
usage = '''Usage: %s file.asm
Convert a music .asm file generated by MIDI2GSC so it does not
depend on pkms.asm'''
print(usage % sys.argv[0], file=sys.stderr)
sys.exit(1)
filename = sys.argv[1]
content = convert(filename)
write(content, filename)
if __name__ == '__main__':
main()