# how do I compute midi stop time

I'm trying to write a program to convert music notation to midi

How do I choose the stop time for a midi note?

Naively I might expect stop_time := start_time + note_duration but for many instruments the note decays far more quickly than the duration. For a drum the note decays very quickly and for an organ not at all.

If I have two notes (crotchet, crotchet) on a piano with no delay in- between I'd end up scheduling the start time of the second note at the same time as the stop time of the first note - but a human being would never play this - there would have to be a physical time delay to allow for finger movement. The note would have also decayed quickly - so I guess I could stop it after (say) 80% of the duration.

Is the stop time really important ?

Any ideas?

• Why don't you use MIDICSV on an already made midi file to find out what you should do? Off the top of my head, I'm pretty sure that start + duration is fine in every case you list as you typically want to hold a note for the full duration, but not every instrument is capable of sustaining it.
– Dom
Commented May 7, 2015 at 17:11

## 6 Answers

Naively I might expect stop_time := start_time + note_duration but for many instruments the note decays far more quickly than the duration. For a drum the note decays very quickly and for an organ not at all.

MIDI doesn't know anything about how it's going to be synthesized. You could take the same MIDI stream and run it through two different patches with different envelopes. This isn't something you should worry about.

but a human being would never play this - there would have to be a physical time delay to allow for finger movement. The note would have also decayed quickly - so I guess I could stop it after (say) 80% of the duration.

Now you're getting into the whole "human playback" can of worms. This is really for you to decide. Try some things and see if they sound right. In this case I would guess that this gap should be a fixed time length, not a percentage of the note duration.

• Great answer! But I would disagree with "this is really for you to decide" in the last paragraph. I know that I myself would be confused and annoyed if an app claimed to "convert notation to MIDI", and then made arbitrary decisions about how it did so. If a note is supposed to last a quarter of a measure, I would expect it to do just that: Make it last 0.25 of the measure, even if that's not how a human would really play it. I would EXPECT to have to MANUALLY humanize it if I wanted it humanized. (This is not a concern AT ALL if there is an explicit "humanize" option in the app itself.) Commented May 7, 2015 at 22:05
• This answer does a good job of pulling apart the two separate aspects of this question (synthesis and performance). It's worth nothing on the synthesis side that, not only might the sound decay before the end of the midi note -- it might also extend beyond the end of the midi note due to reverb effects. But neither of these issues should affect when the NoteOff event occurs, which represents the end of playing a note, not the cessation of sound. As for the performance side, Stephen Hazel's answer is reasonable (default to 100% duration -1 tick, with adjustments for any notated articulations). Commented May 7, 2015 at 23:17
• @CalebHines it's not only reverb effects that could cause the note to extend after the note off - it's also the release time setting on the synthesizer playing the note. Commented Dec 12, 2015 at 8:37
• @topomorto Correct, and a very important point. But I was out of characters. The main point I was illustrating was that the sound could stop on either side of the actual Note Off Event. I probably could have phrased things better. Commented Dec 12, 2015 at 16:57

if you've got a quarter note, you should set the stop time to the start time plus duration minus one "tick".

you can get into the staccato, portato, leggerio, legato if the music specifies it. Legato is full duration (minus a tick so it doesn't mess with the next note). leggerio is the usual 3/4 duration ending. portato is 1/2, stacatto is 1/4 or less.

you shouldn't care about if it's drums or piano or organ. If that's what the sheet music says, that's what the midi file should be.

synthesis comes AFTER the midi is decided and is a whole other can of worms.

• You can't write the MIDI entirely without consideration of the synthesis though. After all, MIDI is simply a set of instructions to the synthesizer, and how the synthesizer interprets those instructions is what makes the actual sound. Commented Dec 12, 2015 at 8:39

You need to have some idea of the volume envelope of the particular synthesizer voice that will be responding to the midi messages.

Usually when you trigger a note-on, the synthesizer attached will play the attack portion of the sound, then the decay portion of the sound, until it reaches the sustain level. When you release the note (sending a note-off), the note will decay (die away) according to the decay time.

With percussive sounds, the attack time is often zero (very fast), and the decay time and release time are often exactly the same. This means that the duration of the MIDI note is actually completely irrelevant - you can send the note-off after a millisecond or a minute, and it will make no difference (apart from some subtleties related to how voices get allocated, but we can ignore that for now).

A piano sound will also have a fast attack, and then start to decay to zero. The release time is then very fast, because the note gets damped when the note is released. I think you are on the right track when you said you need to think about how long a human player would actually hold the note down for. Remember that musical style and playing technique make a difference - in some cases on a piano you might play the notes so long they overlap; in other cases you'd play them short and staccato. Also, you have the option of using MIDI damper/sustain/sostenuto controller messages to extend notes, rather than note-offs - again, depending on how the synthesizer is programmed.

Note that with sample-based synthesizers, you also have to consider the volume envelope inherent in the sample being played, as well as the synthesizer parameters.

But basically : If the sustain level of the synth voice is zero, and the release time and the decay time are the same, it makes no difference when you send the note off. In all other cases, it will make a difference.

• wasn't me, but I'd say because it doesn't address the question of what to actually do with the note-off; only the consequences of when it does occur. Commented May 9, 2015 at 12:33
• @Tetsujin It is a kind of "it all depends" answer - but so's the currently highest voted answer, isn't it? Plus I thought I'd gone into a bit more detail as to what it actually depends on - I have actually written MIDI software before so I was wondering If I'd forgotten what I'd learned and made some schoolboy error! Oh well. Thanks. Commented May 9, 2015 at 14:32
• You might have too much to say on this topic to fit into a single answer, topo morto. Have you thought of asking/answering a series of concocted questions? (Could take days/weeks/months/...) Or have you already? (Didn't look.) Maybe I'll ask them. I'm currently learning MIDI as I live, breathe and type. Commented May 22, 2017 at 21:15
• @musicn to be honest I am now moving in the opposite direction - away from MIDI towards less 'note-based' ways of representing music, but I'll still answer any questions I can... Commented May 22, 2017 at 21:24

Midi note durations correspond to when you press down a key and when you release it again. All the rest of the envelope is the business of the synthesizer interpreting the Midi. "Release" with percussion instruments may mean actively dampening it (obvious with a hihat).

For melody instruments, whether the release reaches the next note, overlaps with it, or stops before it depends on whether you play legato, legatissimo, or leggiero. But it does not notably depend on the instrument type: basically you translate the score into Midi without making the rendition depend on the actual Midi instrument getting used.

• "But it does not notably depend on the instrument type" is incorrect - polyphonic synth voices that have long release times will have overlapping notes if the note-ons come fast enough, regardless of when the note-offs are sent. Commented Dec 12, 2015 at 8:42

Very good question. I too am writing code similar to yours.

I too had wondered about this. For a plucked instrument or a drum the duration of the note is more or less irrelevant. The duration tells me the point in time when the next note starts, not when the current note finishes.

If you take (say) piano music and shorten all notes as say 1/16 notes but make sure the start times are correct, then it won't sound too bad.

I also encountered a problem when programming a Moog monophonic synt using a midi controller - when playing successive notes I sent note on commands immediately after the preceding note off commands, but the synt could not respond quickly enough and some note on's were missed so I turned the notes off a millisecond before the next note on command.

To be honest the midi spec does not help here - If I send two note on messages the same channel and with the same pitch followed by one note off should the note still sound (in the case of an organ) - or should I send two?

It seems to me that a note is a pretty complex thing, it has a printed representation (as in a score) - it has an actual note on and note off time which may or may not correspond to the times implied in the score, and during playback the volume and vibrato of the note can change.

I note that some DAWs (I forget which) permit twiddling the start and stop times within a note in a piano roll editor so that the display shows both the nominal start and stop times (corresponding to what would be a note in a score) and the actual start and stop times (which differ from the score)

To make a piano sound good you also have to take into account the pedal and beat and volume variations implied by phrasing - all this is rather tricky :-)

Send the full crotchet duration. It's the playback instrument's business what it does with it.