genmidi: Update to support Python 3 build.
Tweak the code used to build the GENMIDI Lump so that it properly supports Python 3 for build. Tested use cases were: * Normal build * a2i-to-sbi script to convert AdTrack2 instruments to SBI format * dumpgenmidi script to dump the instruments from a GENMIDI lump. * Running genmidi.py, sbi_file.py, a2i_file.py standalone to print the contents of files in their respective formats.master
parent
8422d38ce4
commit
e47b69a064
|
@ -215,17 +215,19 @@ def decode_type_9(data):
|
|||
# Decode instrument name
|
||||
|
||||
ps = decompressed_data[14:]
|
||||
instr_data["name"], = struct.unpack("%ip" % len(ps), ps)
|
||||
instr_name, = struct.unpack("%ip" % len(ps), ps)
|
||||
instr_data["name"] = instr_name.decode("ascii")
|
||||
|
||||
return instr_data
|
||||
|
||||
def read(filename):
|
||||
f = open(filename)
|
||||
data = f.read()
|
||||
f.close()
|
||||
with open(filename, "rb") as f:
|
||||
data = f.read()
|
||||
|
||||
hdrstr, crc, filever = struct.unpack("<7sHB", data[0:10])
|
||||
|
||||
hdrstr = hdrstr.decode("ascii")
|
||||
|
||||
if hdrstr.lower() != HEADER_STRING.lower():
|
||||
raise Exception("Wrong file header ID string")
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ def instr_to_str_def(filename, filename2, instr):
|
|||
return "Instrument(%s)" % (", ".join(args))
|
||||
|
||||
def print_instr_def(filename, filename2, instr):
|
||||
print "\t%s," % instr_to_str_def(filename, filename2, instr)
|
||||
print("\t%s," % instr_to_str_def(filename, filename2, instr))
|
||||
|
||||
def dump_instrument(filename, filename2, instr):
|
||||
|
||||
|
@ -82,7 +82,7 @@ def dump_instrument(filename, filename2, instr):
|
|||
instr.voice2)
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print >> sys.stderr, "Usage: %s <filename>" % sys.argv[0]
|
||||
sys.stderr.write("Usage: %s <filename>\n" % sys.argv[0])
|
||||
sys.exit(-1)
|
||||
|
||||
instruments = genmidi.read(sys.argv[1])
|
||||
|
@ -90,7 +90,7 @@ instruments = genmidi.read(sys.argv[1])
|
|||
main_instrs = instruments[0:128]
|
||||
percussion = instruments[128:]
|
||||
|
||||
print "INSTRUMENTS = ["
|
||||
print("INSTRUMENTS = [")
|
||||
|
||||
for i in range(len(main_instrs)):
|
||||
instr = main_instrs[i]
|
||||
|
@ -99,9 +99,9 @@ for i in range(len(main_instrs)):
|
|||
dump_instrument(filename, filename2, instr)
|
||||
print_instr_def(filename, filename2, instr)
|
||||
|
||||
print "]"
|
||||
print
|
||||
print "PERCUSSION = ["
|
||||
print("]")
|
||||
print("")
|
||||
print("PERCUSSION = [")
|
||||
|
||||
for i in range(len(percussion)):
|
||||
instr = percussion[i]
|
||||
|
@ -110,5 +110,5 @@ for i in range(len(percussion)):
|
|||
dump_instrument(filename, filename2, instr)
|
||||
print_instr_def(filename, filename2, instr)
|
||||
|
||||
print "]"
|
||||
print("]")
|
||||
|
||||
|
|
|
@ -120,20 +120,19 @@ def encode_instrument_names(instruments):
|
|||
result = []
|
||||
|
||||
for instrument in instruments:
|
||||
result.append(struct.pack("32s", instrument.voice1["name"]))
|
||||
instr_name = instrument.voice1["name"].encode("ascii")
|
||||
result.append(struct.pack("32s", instr_name))
|
||||
|
||||
return b"".join(result)
|
||||
|
||||
def write(filename, instruments):
|
||||
header = struct.pack("%is" % len(GENMIDI_HEADER), GENMIDI_HEADER)
|
||||
header = struct.pack("%is" % len(GENMIDI_HEADER),
|
||||
GENMIDI_HEADER.encode("ascii"))
|
||||
|
||||
f = open(filename, 'w')
|
||||
|
||||
f.write(header)
|
||||
f.write(encode_instruments(instruments))
|
||||
f.write(encode_instrument_names(instruments))
|
||||
|
||||
f.close()
|
||||
with open(filename, "wb") as f:
|
||||
f.write(header)
|
||||
f.write(encode_instruments(instruments))
|
||||
f.write(encode_instrument_names(instruments))
|
||||
|
||||
def decode_voice(data, name):
|
||||
|
||||
|
@ -145,7 +144,7 @@ def decode_voice(data, name):
|
|||
|
||||
result["m_ksl_volume"] = result["m_ksl"] | result["m_volume"]
|
||||
result["c_ksl_volume"] = result["c_ksl"] | result["c_volume"]
|
||||
result["name"] = name.rstrip("\0")
|
||||
result["name"] = name.decode("ascii").rstrip("\0")
|
||||
|
||||
return result
|
||||
|
||||
|
@ -174,14 +173,13 @@ def decode_instrument(data, name):
|
|||
note=fixed_note)
|
||||
|
||||
def read(filename):
|
||||
f = open(filename)
|
||||
data = f.read()
|
||||
f.close()
|
||||
with open(filename, "rb") as f:
|
||||
data = f.read()
|
||||
|
||||
# Check header:
|
||||
|
||||
header = data[0:len(GENMIDI_HEADER)]
|
||||
if header != GENMIDI_HEADER:
|
||||
if header.decode("ascii") != GENMIDI_HEADER:
|
||||
raise Exception("Incorrect header for GENMIDI lump")
|
||||
|
||||
body = data[len(GENMIDI_HEADER):]
|
||||
|
|
|
@ -80,5 +80,5 @@ def def_for_note(note):
|
|||
NOTES = [ "C", "Cs", "D", "Ds", "E", "F", "Fs",
|
||||
"G", "Gs", "A", "As", "B" ]
|
||||
|
||||
return "%s.%s" % (OCTAVES[note / 12], NOTES[note % 12])
|
||||
return "%s.%s" % (OCTAVES[note // 12], NOTES[note % 12])
|
||||
|
||||
|
|
|
@ -53,17 +53,17 @@ FIELDS = [
|
|||
]
|
||||
|
||||
def read(filename):
|
||||
f = open(filename)
|
||||
data = f.read()
|
||||
f.close()
|
||||
with open(filename, "rb") as f:
|
||||
data = f.read()
|
||||
|
||||
header, name = struct.unpack("4s32s", data[0:36])
|
||||
header = header.decode("ascii")
|
||||
|
||||
if header != HEADER_VALUE:
|
||||
raise Exception("Invalid header for SBI file!")
|
||||
|
||||
instr_data = data[36:]
|
||||
result = { "name": name.rstrip("\0") }
|
||||
result = { "name": name.decode("ascii").rstrip("\0") }
|
||||
|
||||
for i in range(len(FIELDS)):
|
||||
result[FIELDS[i]], = struct.unpack("B", instr_data[i:i+1])
|
||||
|
@ -71,16 +71,14 @@ def read(filename):
|
|||
return result
|
||||
|
||||
def write(filename, data):
|
||||
f = open(filename, 'w')
|
||||
with open(filename, "wb") as f:
|
||||
f.write(struct.pack("4s", HEADER_VALUE.encode("ascii")))
|
||||
f.write(struct.pack("32s", data["name"].encode("ascii")))
|
||||
|
||||
f.write(struct.pack("4s32s", HEADER_VALUE, data["name"]))
|
||||
|
||||
for field in FIELDS:
|
||||
f.write(struct.pack("B", data[field]))
|
||||
for x in range(16 - len(FIELDS)):
|
||||
f.write(struct.pack("B", 0))
|
||||
|
||||
f.close()
|
||||
for field in FIELDS:
|
||||
f.write(struct.pack("B", data[field]))
|
||||
for x in range(16 - len(FIELDS)):
|
||||
f.write(struct.pack("B", 0))
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
|
|
Loading…
Reference in New Issue