I am writing a simple midi file using music21:
import music21
stream = music21.stream.Stream()
n = music21.note.Note(60)
n.duration.type = 'half'
stream.insert(0, n)
stream.write('midi', 'out.midi')
This plays middle c for half a measure. However, the resulting file contains empty space for the remainder of that measure:
Is there a way to remove that trailing "whitespace"?
The current version from PyPI (7.1.0) seems to always add one quarter note of silence at the end. This can be changed by setting music21.defaults.ticksAtStart
, despite its name. The units are MIDI ticks defined by music21.defaults.ticksPerQuarter
. The default value of both is 1024.
Be aware that some MIDI players will stop synthesizing when the file ends, so the sound will stop dead when the last note is released instead of dying away if there is no padding.
Also, some players may pad to a measure boundary no matter what you do. There is no way to encode a pickup measure in MIDI, other than inserting rests.