简体   繁体   中英

Music21: Strip empty data at end of written midi file

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM