While I agree with Brandefly, I am more programmer then musician. And we are so lazy, we prefer to make computers do the work for us, even if we need to "teach" them first
Also I can imagine that someone has extract tempo from some other free played audio source (guitar, drums, etc) but recorded MIDI listening it, without the tempo map applied first.
And so my usual link:
http://www.azslow.com/index.php/topic,286.0.html"Apply tempo map" preset is now included into Examples attachment (at the end of the thread). There you can also look at the source code, if you are interested how it works.
How I have tested it:
1) with click turned off, I have played on my (digital) piano and recorded the result as MIDI in Sonar
2) rendered that MIDI with TruePiano plug-in, exported WAV
3) loaded it into Melodyne, extracted tempo map
4) opened tempo map MIDI as different project, selected "all" and "Copy special..." with Tempo selected
5) back to the original project, "Paste special..." that tempo map. So, rendered audio is still the same, tempo map is following it, MIDI clip is out of sync...
6) extended MIDI clip to be a bit longer then shown audio, selected it, right click "Process Effect"/"MIDI Effects"/"AZ Lua". Selected "Example: Apply tempo map" preset, checked BPM (it was default for Sonar 100), pressed "Ok"
7) MIDI clip is perfectly in sync again (checked with rendered audio in parallel to MIDI+VSTi)