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.
This commit is contained in:
Simon Howard 2014-10-28 03:55:22 +00:00
parent 8422d38ce4
commit e47b69a064
5 changed files with 37 additions and 39 deletions

View file

@ -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):]