mirror of
https://github.com/freedoom/freedoom.git
synced 2025-09-02 07:25:45 -04:00
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:
parent
8422d38ce4
commit
e47b69a064
5 changed files with 37 additions and 39 deletions
|
@ -215,17 +215,19 @@ def decode_type_9(data):
|
||||||
# Decode instrument name
|
# Decode instrument name
|
||||||
|
|
||||||
ps = decompressed_data[14:]
|
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
|
return instr_data
|
||||||
|
|
||||||
def read(filename):
|
def read(filename):
|
||||||
f = open(filename)
|
with open(filename, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
hdrstr, crc, filever = struct.unpack("<7sHB", data[0:10])
|
hdrstr, crc, filever = struct.unpack("<7sHB", data[0:10])
|
||||||
|
|
||||||
|
hdrstr = hdrstr.decode("ascii")
|
||||||
|
|
||||||
if hdrstr.lower() != HEADER_STRING.lower():
|
if hdrstr.lower() != HEADER_STRING.lower():
|
||||||
raise Exception("Wrong file header ID string")
|
raise Exception("Wrong file header ID string")
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,7 @@ def instr_to_str_def(filename, filename2, instr):
|
||||||
return "Instrument(%s)" % (", ".join(args))
|
return "Instrument(%s)" % (", ".join(args))
|
||||||
|
|
||||||
def print_instr_def(filename, filename2, instr):
|
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):
|
def dump_instrument(filename, filename2, instr):
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ def dump_instrument(filename, filename2, instr):
|
||||||
instr.voice2)
|
instr.voice2)
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
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)
|
sys.exit(-1)
|
||||||
|
|
||||||
instruments = genmidi.read(sys.argv[1])
|
instruments = genmidi.read(sys.argv[1])
|
||||||
|
@ -90,7 +90,7 @@ instruments = genmidi.read(sys.argv[1])
|
||||||
main_instrs = instruments[0:128]
|
main_instrs = instruments[0:128]
|
||||||
percussion = instruments[128:]
|
percussion = instruments[128:]
|
||||||
|
|
||||||
print "INSTRUMENTS = ["
|
print("INSTRUMENTS = [")
|
||||||
|
|
||||||
for i in range(len(main_instrs)):
|
for i in range(len(main_instrs)):
|
||||||
instr = main_instrs[i]
|
instr = main_instrs[i]
|
||||||
|
@ -99,9 +99,9 @@ for i in range(len(main_instrs)):
|
||||||
dump_instrument(filename, filename2, instr)
|
dump_instrument(filename, filename2, instr)
|
||||||
print_instr_def(filename, filename2, instr)
|
print_instr_def(filename, filename2, instr)
|
||||||
|
|
||||||
print "]"
|
print("]")
|
||||||
print
|
print("")
|
||||||
print "PERCUSSION = ["
|
print("PERCUSSION = [")
|
||||||
|
|
||||||
for i in range(len(percussion)):
|
for i in range(len(percussion)):
|
||||||
instr = percussion[i]
|
instr = percussion[i]
|
||||||
|
@ -110,5 +110,5 @@ for i in range(len(percussion)):
|
||||||
dump_instrument(filename, filename2, instr)
|
dump_instrument(filename, filename2, instr)
|
||||||
print_instr_def(filename, filename2, instr)
|
print_instr_def(filename, filename2, instr)
|
||||||
|
|
||||||
print "]"
|
print("]")
|
||||||
|
|
||||||
|
|
|
@ -120,20 +120,19 @@ def encode_instrument_names(instruments):
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for instrument in instruments:
|
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)
|
return b"".join(result)
|
||||||
|
|
||||||
def write(filename, instruments):
|
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')
|
with open(filename, "wb") as f:
|
||||||
|
f.write(header)
|
||||||
f.write(header)
|
f.write(encode_instruments(instruments))
|
||||||
f.write(encode_instruments(instruments))
|
f.write(encode_instrument_names(instruments))
|
||||||
f.write(encode_instrument_names(instruments))
|
|
||||||
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def decode_voice(data, name):
|
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["m_ksl_volume"] = result["m_ksl"] | result["m_volume"]
|
||||||
result["c_ksl_volume"] = result["c_ksl"] | result["c_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
|
return result
|
||||||
|
|
||||||
|
@ -174,14 +173,13 @@ def decode_instrument(data, name):
|
||||||
note=fixed_note)
|
note=fixed_note)
|
||||||
|
|
||||||
def read(filename):
|
def read(filename):
|
||||||
f = open(filename)
|
with open(filename, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
# Check header:
|
# Check header:
|
||||||
|
|
||||||
header = data[0:len(GENMIDI_HEADER)]
|
header = data[0:len(GENMIDI_HEADER)]
|
||||||
if header != GENMIDI_HEADER:
|
if header.decode("ascii") != GENMIDI_HEADER:
|
||||||
raise Exception("Incorrect header for GENMIDI lump")
|
raise Exception("Incorrect header for GENMIDI lump")
|
||||||
|
|
||||||
body = data[len(GENMIDI_HEADER):]
|
body = data[len(GENMIDI_HEADER):]
|
||||||
|
|
|
@ -80,5 +80,5 @@ def def_for_note(note):
|
||||||
NOTES = [ "C", "Cs", "D", "Ds", "E", "F", "Fs",
|
NOTES = [ "C", "Cs", "D", "Ds", "E", "F", "Fs",
|
||||||
"G", "Gs", "A", "As", "B" ]
|
"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):
|
def read(filename):
|
||||||
f = open(filename)
|
with open(filename, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
header, name = struct.unpack("4s32s", data[0:36])
|
header, name = struct.unpack("4s32s", data[0:36])
|
||||||
|
header = header.decode("ascii")
|
||||||
|
|
||||||
if header != HEADER_VALUE:
|
if header != HEADER_VALUE:
|
||||||
raise Exception("Invalid header for SBI file!")
|
raise Exception("Invalid header for SBI file!")
|
||||||
|
|
||||||
instr_data = data[36:]
|
instr_data = data[36:]
|
||||||
result = { "name": name.rstrip("\0") }
|
result = { "name": name.decode("ascii").rstrip("\0") }
|
||||||
|
|
||||||
for i in range(len(FIELDS)):
|
for i in range(len(FIELDS)):
|
||||||
result[FIELDS[i]], = struct.unpack("B", instr_data[i:i+1])
|
result[FIELDS[i]], = struct.unpack("B", instr_data[i:i+1])
|
||||||
|
@ -71,16 +71,14 @@ def read(filename):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def write(filename, data):
|
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 field in FIELDS:
|
for x in range(16 - len(FIELDS)):
|
||||||
f.write(struct.pack("B", data[field]))
|
f.write(struct.pack("B", 0))
|
||||||
for x in range(16 - len(FIELDS)):
|
|
||||||
f.write(struct.pack("B", 0))
|
|
||||||
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
for filename in sys.argv[1:]:
|
for filename in sys.argv[1:]:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue