forked from m13253/midi-track-merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mididump
executable file
·154 lines (144 loc) · 6.39 KB
/
mididump
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
#!/usr/bin/env python3
import json
import struct
import sys
def ReadUntil(f, buf):
buf = bytes(buf)
if not len(buf):
return
while True:
t = f.read(1)
if not t:
raise EOFError
if buf.startswith(t):
pos = f.tell()
if f.read(len(buf) - 1) == buf[1:]:
return
f.seek(pos, 0)
def ReadOrEOF(f, size):
b = f.read(size)
if len(b) == size:
return b
else:
raise EOFError
def ScanBigInt(f, prefix=b''):
res = 0
count = -len(prefix)
while True:
if prefix:
t = struct.unpack('>B', prefix[0:1])[0]
prefix = prefix[1:]
else:
t = struct.unpack('>B', ReadOrEOF(f, 1))[0]
count += 1
if t & 0x80:
res = (res << 7) | (t & 0x7f)
else:
return ((res << 7) | t, count)
def main(fn):
sys.stdout.write('<!--\n Output of mididump, a simple MIDI dump tool.\n vim: ft=xml\n-->\n')
f = open(fn, "rb")
ReadUntil(f, b'MThd')
mthd_len = struct.unpack('>L', ReadOrEOF(f, 4))[0]
mthd = struct.unpack('>HHH', ReadOrEOF(f, 6))
sys.stdout.write('%08x <mthd size="%d" type="%d" tracks="%d" division="%d">\n' % ((f.tell() - 14, mthd_len) + mthd))
for trkno in range(mthd[1]):
sys.stdout.write('%08x ' % f.tell())
mtrk = ReadOrEOF(f, 4)
assert mtrk == b'MTrk'
mtrk_len = struct.unpack('>L', ReadOrEOF(f, 4))[0]
sys.stdout.write('<mtrk size="%d">\n' % mtrk_len)
mtrk_len_read = 0
last_time = 0
last_meta = 0x80
while mtrk_len_read < mtrk_len:
sys.stdout.write('%08x ' % f.tell())
timestamp = ScanBigInt(f)
mtrk_len_read += timestamp[1]
if mtrk_len_read >= mtrk_len:
raise EOFError
last_time += timestamp[0]
meta = ReadOrEOF(f, 1)
mtrk_len_read += 1
meta_int = struct.unpack('>B', meta)[0]
if mtrk_len_read >= mtrk_len:
raise EOFError
if meta_int & 0x80:
last_meta, param = meta, ReadOrEOF(f, 1)
mtrk_len_read += 1
meta_int = struct.unpack('>B', meta)[0]
else:
meta, param = last_meta, meta
meta_int = struct.unpack('>B', meta)[0]
if meta_int == 0xff:
meta_len = ScanBigInt(f)
mtrk_len_read += meta_len[1]
if mtrk_len_read > mtrk_len:
raise EOFError
meta_data = ReadOrEOF(f, meta_len[0])
mtrk_len_read += meta_len[0]
if mtrk_len_read > mtrk_len:
raise EOFError
sys.stdout.write(' <meta time="%d/%d" event="0x%02x" type="0x%02x" len="%d" data=' % (last_time, mthd[2], meta_int, param[0], meta_len[0]))
if param[0] == 0x51:
tempo = 0
for tempoi in meta_data:
tempo = (tempo << 8) | tempoi
sys.stdout.write('"%.2f" />\n' % (60000000.0 / tempo))
elif param[0] in (0x54, 0x58):
sys.stdout.write('"%s" />\n' % ' '.join([str(meta_datai) for meta_datai in meta_data]))
elif param[0] == 0x59 and meta_len[0] == 2:
if meta_data[1] == 0:
if 0 <= meta_data[0] <= 7:
sys.stdout.write('"%s Maj" />\n' % ('C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#')[meta_data[0]])
elif 249 <= meta_data[0] <= 255:
sys.stdout.write('"%s Maj" />\n' % ('Cb', 'Gb', 'Db', 'Ab', 'Eb', 'Bb', 'F')[meta_data[0] - 249])
else:
sys.stdout.write('"%s Maj" />\n' % meta_data[0])
elif meta_data[1] == 1:
if 0 <= meta_data[0] <= 7:
sys.stdout.write('"%s min" />\n' % ('A', 'E', 'B', 'F#', 'C#', 'G#', 'D#', 'A#')[meta_data[0]])
elif 249 <= meta_data[0] <= 255:
sys.stdout.write('"%s min" />\n' % ('Ab', 'Eb', 'Bb', 'F', 'C', 'G', 'D')[meta_data[0] - 249])
else:
sys.stdout.write('"%s min" />\n' % meta_data[0])
else:
sys.stdout.write('"%s %s" />\n' % (meta_data[0], meta_data[1]))
else:
sys.stdout.write('%s />\n' % json.dumps(meta_data.decode('utf-8', 'replace'), ensure_ascii=False))
elif meta_int & 0xf0 == 0xf0:
meta_len = ScanBigInt(f, param)
mtrk_len_read += meta_len[1]
if mtrk_len_read > mtrk_len:
raise EOFError
meta_data = ReadOrEOF(f, meta_len[0])
mtrk_len_read += meta_len[0]
if mtrk_len_read > mtrk_len:
raise EOFError
sys.stdout.write(' <sysex time="%d/%d" event="0x%02x" len="%d" param=%s />\n' % (last_time, mthd[2], meta_int, meta_len[0], json.dumps(meta_data.decode('utf-8', 'replace'), ensure_ascii=False)))
else:
sys.stdout.write(' <event time="%d/%d" event="0x%x" channel="%2d" param="' % (last_time, mthd[2], meta_int >> 4, (meta_int & 0xf) + 1))
if mtrk_len_read > mtrk_len:
raise EOFError
if meta_int >> 4 not in (0xc, 0xd):
param += ReadOrEOF(f, 1)
mtrk_len_read += 1
if meta_int >> 4 in (0x8, 0x9, 0xa):
sys.stdout.write('%-3s %d" />\n' % ('%s%d' % (('C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B')[param[0] % 12], param[0] / 12 - 1), param[1]))
elif len(param) == 2:
sys.stdout.write('0x%02x %d" />\n' % (param[0], param[1]))
else:
sys.stdout.write('%d" />\n' % param[0])
sys.stdout.write('%08x </mtrk>\n' % f.tell())
sys.stdout.write('%08x </mthd>\n' % f.tell())
f.close()
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.stderr.write('Simple MIDI dump tool.\nUsage: %s foo.mid\n\n' % sys.argv[0])
sys.exit(1)
else:
try:
sys.exit(main(sys.argv[1]))
except Exception as e:
raise
sys.stderr.write('Error: %s %s\n' % (type(e).__name__, e))