-
Notifications
You must be signed in to change notification settings - Fork 6
/
vgmdump.py
261 lines (197 loc) · 7.51 KB
/
vgmdump.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
#!/usr/bin/env python
# vgmdump.py
# Tool for dumping the contents of a VGM file in an interleaved raw register dump format
# By Simon Morris (https://github.com/simondotm/)
# See https://github.com/simondotm/vgm-packer
#
# Copyright (c) 2019 Simon Morris. All rights reserved.
#
# "MIT License":
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import functools
import itertools
import struct
import sys
import time
import binascii
import math
import operator
import os
from modules.vgmparser import VgmStream
class VgmDump:
def __init__(self):
print("init")
#----------------------------------------------------------
# Utilities
#----------------------------------------------------------
# split the packed raw data into 11 separate streams
# returns array of 11 bytearrays
def split_raw(self, rawData, stripCommands = True):
registers = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
registers_opt = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
latched_channel = -1
output_block = bytearray()
output_blocks = []
for o in range(11):
output_blocks.append( bytearray() )
if stripCommands:
register_mask = 15
else:
register_mask = 255
# unpack the raw binary data in 11 arrays of register data without any deltas between them
# eg. the raw chip writes to all 11 registers every frame
n = 0
Packet = True
verbose = False
while (Packet):
packet_size = rawData[n]
if verbose:
print("packet_size=" + str(packet_size))
n += 1
if packet_size == 255:
Packet = False
else:
for x in range(packet_size):
d = rawData[n+x]
#if verbose:
# print " frame byte number=" +str(x)
# print " frame byte=" +str(d)
if d & 128:
# latch
c = (d>>5)&3
latched_channel = c
if d & 16:
# volume
if verbose:
print(" volume on channel " + str(c))
registers[c+7] = d & register_mask
else:
# tone
if verbose:
print(" tone on channel " + str(c))
registers[c*2+0] = d & register_mask
else:
if verbose:
print(" tone data on latched channel " + str(latched_channel))
registers[latched_channel*2+1] = d # we no longer do any masking here # d & 63 # tone data only contains 6 bits of info anyway, so no need for mask
if latched_channel == 3:
print("ERROR CHANNEL")
# emit current state of each of the 11 registers to 11 different bytearrays
for x in range(11):
output_blocks[x].append( registers[x] )
# next packet
n += packet_size
# Add EOF marker (0x08) to tone3 byte stream
output_blocks[6].append(0x08) # 0x08 is an invalid noise tone.
# return the split blocks
return output_blocks
# from the 11 registers array, return a new bytearray of the registers combined from the given array
# where combination is an array, eg. [0,3,1]
def combine_registers(self, registers, combination):
buffer = bytearray()
for x in range(len(registers[0])):
for y in range(len(combination)):
r = combination[y]
buffer.append( registers[r][x] )
return buffer
# from the 11 registers array, return a new byte array which is all 11 registers sets combined into one buffer
def combine_parts(self, registers):
buffer = bytearray()
for x in range(len(registers)):
buffer += registers[x]
return buffer
#----------------------------------------------------------
# Process(filename)
# Convert the given VGM file to a dump file
#----------------------------------------------------------
def process(self, src_filename, dst_filename, add_header = False):
# load the VGM file, or alternatively interpret as a binary
if src_filename.lower()[-4:] == ".vgm":
vgm = VgmStream(src_filename)
data_block = vgm.as_binary()
else:
fh = open(src_filename, 'rb')
data_block = bytearray(fh.read())
fh.close()
data_offset = 0
# parse the header
header_size = data_block[0] # header size
play_rate = data_block[1] # play rate
if header_size == 5 and play_rate == 50:
packet_count = data_block[2] + data_block[3]*256 # packet count LO
duration_mm = data_block[4] # duration mm
duration_ss = data_block[5] # duration ss
data_offset = header_size+1
data_offset += data_block[data_offset]+1
data_offset += data_block[data_offset]+1
print("header_size=" +str(header_size))
print("play_rate="+str(play_rate))
print("packet_count="+str(packet_count))
print("duration_mm="+str(duration_mm))
print("duration_ss="+str(duration_ss))
print("data_offset="+str(data_offset))
else:
print("No header.")
print("")
# stash the header
header = data_block[:data_offset]
# Trim off the header data. The rest is raw data.
data_block = data_block[data_offset:]
#----------------------------------------------------------
# Begin VGM dump
#----------------------------------------------------------
#----------------------------------------------------------
# Unpack the register data into 11 separate data streams
#----------------------------------------------------------
registers = self.split_raw(data_block, False)
dump = self.combine_registers(registers, [0,1,2,3,4,5,6,7,8,9,10])
# write a raw data version of the file
fh = open(dst_filename, "wb")
if add_header:
print("Writing Header...")
fh.write(header)
fh.write( dump )
#------------------------------------------------------------------------
# Main()
#------------------------------------------------------------------------
import argparse
# Determine if running as a script
if __name__ == '__main__':
print("VgmDump.py : VGM file dump utility")
print("Written in 2019 by Simon Morris, https://github.com/simondotm/vgm-packer")
print("")
epilog_string = ""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=epilog_string)
parser.add_argument("input", help="VGM source file (must be single SN76489 PSG format) [input]")
parser.add_argument("-o", "--output", metavar="<output>", help="write VGC file <output> (default is '[input].raw')")
parser.add_argument("-m", "--meta", help="Output metadata header", action="store_true")
parser.add_argument("-v", "--verbose", help="Enable verbose mode", action="store_true")
args = parser.parse_args()
src = args.input
dst = args.output
if dst == None:
dst = os.path.splitext(src)[0] + ".raw"
# check for missing files
if not os.path.isfile(src):
print("ERROR: File '" + src + "' not found")
sys.exit()
dumper = VgmDump()
dumper.VERBOSE = args.verbose
dumper.process(src, dst, args.meta)