I am writing some custom Unix/Perl programs to handle batch processing of MIDI files, and I have a question about aftertouch that I have not been able to find by searching the internet.

(By the way, assume that all the MIDI data I am talking about is generated by some kind of notation program, so when I say "play a note" I mean "send a 'note on' command" -- I am not "playing" the notes on a real keyboard. Also, we can safely assume that there is no aftertouch data in my original MIDI files -- just 'note on' and 'note off' commands. And for the sake of making things simple, assume that when I say "all notes" I really mean "all notes on a specific channel".)

Does aftertouch (both polyphonic aftertouch and channel aftertouch) have a "default" or "reset" value? Perhaps what I mean by "default/reset" value is best explained by using pitch bend as an example:

At the start of a MIDI file, pitch bend defaults to the mid-point (no bend). When I change the pitch bend (let's say down a half step), it stays that way for the rest of the file until I change it to another value. If I want to bend just one note, then after that note I would reset the pitch bend back to the mid-point (no bend) so that the following notes are not bent. No problem -- I understand that.

But how does aftertouch work in that regard?

Let's start with polyphonic aftertouch:

When I send a 'note on' command, is there a default value of aftertouch associated with it? (0? 64? 127? -- or does the note simply have "no" aftertouch.) Or is the initial aftertouch value the same as the key velocity with which the note was played? (This seems the most likely answer to me, but I am not sure.)

Let's say I send a 'note on' command, followed by an aftertouch command. Does the aftertouch need to be reset before the next note is played (similar to pitch bend), or does the aftertouch value die when the note receives the 'note off' command, so that the following note will have the "default" value of aftertouch applied? (Note: I realize that polyphonic aftertouch is specific to the note number to which it was assigned, so aftertouch assigned to note 60 should not affect any other note numbers -- in the previous sentence, when I say "following note", I mean the next note with the same note number.)

Assuming that the polyphonic aftertouch value just dies with the 'note off' command, does it work the same way for channel aftertouch? In other words, does the assigned aftertouch value remain in effect until all notes that were playing receive the 'note off' command? What if additional notes (which were not playing when the aftertouch value was set) receive the 'note on' command before the notes that were playing have received 'note off'? I assume that the channel aftertouch value should apply to those notes also, which kind of implies that the aftertouch value would die/reset only when all notes on the channel have received the 'note off' command.

Or is channel aftertouch somehow tied to the velocities of notes. If I generate a note with velocity 60, and then set channel aftertouch to 90, what happens if I generate a note with velocity 100 while the other note is still playing? Since channel aftertouch is really just an average of pressure on all keys, does the value of 100 help "pull up" the channel aftertouch, or is the velocity of the second note completely independent of channel aftertouch?

Hope this wasn't too stupid a question. I really do understand a lot about MIDI. But as a programmer, I want to know all the nitty-gritty details, so that I can write programs that work as expected. And this is a question I just couldn't find an answer to.