Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour for short (zero-duration) notes #161

Open
cifkao opened this issue Feb 5, 2019 · 2 comments
Open

Inconsistent behaviour for short (zero-duration) notes #161

cifkao opened this issue Feb 5, 2019 · 2 comments

Comments

@cifkao
Copy link

cifkao commented Feb 5, 2019

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.

@craffel
Copy link
Owner

craffel commented Feb 5, 2019

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.

@00sapo
Copy link

00sapo commented Mar 31, 2021

I propose a flag to give the user the ability to write such short notes with the minimum duration available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants