freedoom/lumps/dmxgus/gather_stats.py
Mike Swanson 6eef9be73a use python3 only for building
Python 2 is very near end-of-life, and Python3-compatible changes to a
few scripts introduced compatibility problems with 2.7 again.  It went
unnoticed for me since my system symlinks "python" to "python3", but
it broke the build on systems where that symlink is still python2.  At
this point in time, I feel it is worth targetting modern Python and
forgetting about 2.7.
2019-09-06 14:43:50 -07:00

55 lines
1.5 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
#
# Script to gather statistics from MIDI files about instrument usage.
# Generates stats.py which is used as input for the ultramid.ini
# generator script.
#
# This script requires python-midi; see here:
# https://github.com/vishnubob/python-midi
#
import midi
import sys
def get_instr_stats(filename):
"""Get a set of instruments used by the specified MIDI file."""
result = set()
midfile = midi.read_midifile(filename)
for track in midfile:
for event in track:
if (
isinstance(event, midi.ProgramChangeEvent)
and event.channel != 9
):
instr = event.data[0]
result.add(instr)
# Percussion:
if isinstance(event, midi.NoteOnEvent) and event.channel == 9:
instr = event.data[0] + 128
result.add(instr)
return result
total_stats = [0] * 217
for filename in sys.argv[1:]:
print("Processing %s" % filename)
stats = get_instr_stats(filename)
print(sorted(stats))
for instrument in stats:
total_stats[instrument] += 1
with open("stats.py", "w") as f:
f.write("# Instrument stats, autogenerated by gather_stats.py\n\n")
f.write("INSTRUMENT_STATS = [\n\t")
for index, stat in enumerate(total_stats):
f.write("% 5i," % stat)
if (index % 10) == 9:
f.write("\n\t")
f.write("\n]\n")