freedoom/lumps/genmidi/dumpgenmidi
Simon Howard a94ae50ffb genmidi: Better dumpgenmidi output.
This adds dumping for the tune= field that was just added in commit
276118107, but also:

* Instrument name gets put in a comment at the end of line - like the
  current configuration in config.py.
* Instrument calls get split across two lines when there are many
  parameters, to try to keep to 80 columns.
* off1= and off2= parameters now get outputted as +x or -x rather than
  just x or -x.
2023-06-24 00:50:53 -04:00

110 lines
2.7 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
#
# Dump the contents of a GENMIDI lump into separate .sbi instrument files,
# and output a config to stdout.
#
import os
import sys
import genmidi
import sbi_file
import midi
import sbi_file
def is_null_voice(voice_data):
for f in sbi_file.FIELDS:
if voice_data[f] != 0:
return False
return True
def instr_to_str_def(filename, filename2, instr):
if is_null_voice(instr.voice1):
return "NullInstrument"
file_args = "%r" % filename
if instr.voice2 is not None:
file_args += ", %r" % filename2
instr_name = instr.voice1["name"]
extra_args = []
if instr.fixed_note is not None:
extra_args.append("note=%s" % midi.def_for_note(instr.fixed_note))
if instr.offset1 != 0:
extra_args.append("off1=%+i" % instr.offset1)
if instr.offset2 != 0:
extra_args.append("off2=%+i" % instr.offset2)
if instr.tune != 0:
extra_args.append("tune=%+i" % instr.tune)
extra_args = ", ".join(extra_args)
if extra_args:
file_args += ", "
if len(file_args + extra_args) <= 32:
return "\tInstrument(%s%s), %s# %s" % (
file_args, extra_args,
" " * (32 - len(file_args + extra_args)),
instr_name)
return "\tInstrument(%s %s# %s\n\t %s)," % (
file_args.strip(),
" " * (35 - len(file_args)), instr_name,
extra_args)
def print_instr_def(filename, filename2, instr):
print(instr_to_str_def(filename, filename2, instr))
def dump_instrument(filename, filename2, instr):
if is_null_voice(instr.voice1):
return
sbi_file.write(os.path.join("instruments", filename), instr.voice1)
if instr.voice2 is not None:
sbi_file.write(os.path.join("instruments", filename2), instr.voice2)
if len(sys.argv) != 2:
sys.stderr.write("Usage: %s <filename>\n" % sys.argv[0])
sys.exit(-1)
instruments = genmidi.read(sys.argv[1])
main_instrs = instruments[0:128]
percussion = instruments[128:]
print("# Note: all instruments must ALWAYS be defined, or use NullInstrument.")
print("")
print("# General MIDI instruments:")
print("INSTRUMENTS = [")
for i in range(len(main_instrs)):
instr = main_instrs[i]
filename = "instr%03i.sbi" % (i + 1)
filename2 = "instr%03i-2.sbi" % (i + 1)
dump_instrument(filename, filename2, instr)
print_instr_def(filename, filename2, instr)
print("]")
print("")
print("# Percussion instruments:")
print("PERCUSSION = [")
for i in range(len(percussion)):
instr = percussion[i]
filename = "perc%02i.sbi" % (i + 35)
filename2 = "perc%02i-2.sbi" % (i + 35)
dump_instrument(filename, filename2, instr)
print_instr_def(filename, filename2, instr)
print("]")