why do the ę and ą sometimes sound as "en" and "an" and sometimes (in the end of a word) like "e" and "a"?
"In Polish ę comes after e in the alphabet but never appears at the start of a word. It is most commonly pronounced as /ɛw̃/, /ɛn/, /ɛm/, or /ɛ/, depending on the context.
Unlike French, nasal vowels in Polish are asynchronous, meaning that they are pronounced as an oral vowel + a nasal semivowel [ɛw̃], or a nasal vowel + a nasal semivowel. For the sake of simplicity, it is sometimes transcribed [ɛ̃]."
In other words, hearing en/em or an/am is OK, it is just an artefact of a nasal semivowel. As for word final nasals, in Polish there is word final devoicing(with a few exceptions) which means that word final nasal vowel might be realised "not-fully" nasally, that is the semivowel might be lost. ;-) It is particularly strong in case of word-final "ę", which is also replaced (either in word-final position or totally) by em or e in certain dialects and that leads to widely varying levels of nasality among speakers of Polish – some don't do it at all in word-final position, others do something between "e" and "ę", yet others produce "full-blown" "ę"(but that is widely considered as an error of hypercorrection).
As for word-final "ą", it also varies, but realising it fully "flat"(i.e. as "a") is always perceived as wrong – if the TTS is doing it, you should report that as a wrong audio.