- Assign a prior probability of letter frequencies based on a corpus of the language text you're analyzing.
- Separate the different keystrokes in the audio file into a series of times between keystrokes. (i.e, have your program recognize one keystroke as distinct from another)
- Based on the subtle timing differences between keystrokes, assign various lengths to different timings.
- Using the prior probability of the letter frequencies, assign the different time lengths to different characters based on their frequencies.
- You now have a straightforward mapping between the distance between two keystrokes and the character typed, which should allow you to decode the typed text.
Further calibration can probably be had by considering a word dictionary and using fuzzy matching to detect how often words are decoded incorrectly and what the correct decoding would be.
Of course, if you know or suspect that you are under such surveillance, you could try and alter your typing cadence, e.g. by switching to hunt-and-peck.
Maybe a random layout generator that would mask this effect and only cost typing speed for the extra defense?
Also worth mentioning that just like the Secret Service has an ink database on all the printer types in the world, the NSA is supposed to have a database of what different keyboards sound like. This means that simply by recording the sound of you typing, they can infer keystrokes / characters. Obviously the easiest way to record this is by hacking your phone, which is right next to you.
https://en.m.wikipedia.org/wiki/Van_Eck_phreaking