Good job. A small problem, though...
You use
tempfile.mkstemp, which
creates a temp file. Then you use the name for output file. This will be opened later, and since the temp file already exists MMA generates a message:
Overwriting existing midi file (12 bars, 0.40 min / 0:23 m:s): '/tmp/MMA_1t45x9r1.mid'If you do not want to use
tempfile.mktemp (for good reasons) it may be worth to consider something similar to:
f = tempfile.NamedTemporaryFile(prefix="MMA_", suffix=".mid")
outfile = f.name
f.close # file will be deletedShould some complain that the output file is opened twice, you can blame python
.