pretty-midi
pretty-midi copied to clipboard
Inconsistent behaviour for short (zero-duration) notes
Notes whose duration is below the resolution of the MIDI file get written as a note-on and a note-off occurring at the same time. This causes "spurious long-running notes" as described in #131.
Example:
instr = pretty_midi.Instrument(0)
instr.notes.append(pretty_midi.Note(start=0., end=0.0001, pitch=60, velocity=127))
instr.notes.append(pretty_midi.Note(start=1., end=2., pitch=60, velocity=127))
midi = pretty_midi.PrettyMIDI()
midi.instruments.append(instr)
midi.write('/tmp/test.midi')
midi2 = pretty_midi.PrettyMIDI('/tmp/test.midi')
midi2.instruments[0].notes
# [Note(start=0.000000, end=2.000000, pitch=60, velocity=127),
# Note(start=1.000000, end=2.000000, pitch=60, velocity=127)]
Here, the note-on and note-off at time 0 are interpreted in the reverse order, which creates the spurious long note from 0.0 to 2.0. Inspecting the file using mido:
mf = mido.MidiFile('/tmp/test.midi')
list(mf.tracks[1])
# [<message program_change channel=0 program=0 time=0>,
# <message note_on channel=0 note=60 velocity=0 time=0>,
# <message note_on channel=0 note=60 velocity=127 time=0>,
# <message note_on channel=0 note=60 velocity=127 time=440>,
# <message note_on channel=0 note=60 velocity=0 time=440>,
# <meta message end_of_track time=1>]
I don't know what the best solution is, maybe ignoring such notes (i.e. not writing them) or removing them in remove_invalid_notes.
Thanks for noticing this.
removing them in
remove_invalid_notes.
I think this is the correct solution, maybe with a warning that a note with duration < resolution has been ignored.
I propose a flag to give the user the ability to write such short notes with the minimum duration available