There have been numerous solutions posted on the internet for this issue, however many of these answers appears to be for older software, with possibly outdated methods. I was wandering what the best current way to sync a midi to an audio so that every measure would be in sync.
The only way to do this is to build a 'tempo track' in your 'DAW' of choice so that bar-lines appear in exactly the correct spot in relation to the audio. Once you have done this (and it takes time) you will then be able to build midi events that work in relation to the tempo track and they will be in sync with the audio track.
This Stack Overflow answer may help: as it says, use almost any modern DAW to detect the transients in the audio to build a tempo map. I've also seen the product alluded to in the SO answer, but haven't paid to try it (looking for a free program brought me here).