I am saving text-to-speech audio to file using the following code:
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
stream.Open('audio.mp3', SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
Occasionally, the file created cannot be played and may have size zero - although no errors occur. I found an example of input text that was used during one of these failures (too long to include here) and the result was reproducible, but it has no obviously problematic features.
I haven't been able to find out how to detect errors in the code above or determine what might be causing the problem. Ideally, I would like to resolve the issue, but detecting the problem to enable me to deal with it would suffice. How can I achieve this?