Ninjakannon
Ninjakannon

Reputation: 3819

Python comtypes text-to-speech failing silently

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?

Upvotes: 2

Views: 2844

Answers (1)

IronManMark20
IronManMark20

Reputation: 1338

Well, I don't know why it failed silently, but when I ran it, it raised: ImportError: Cannot import name SpeechLib.

I then went to trusty ole Google, and found this SO question:

Can't save to wav from python, can't import SpeechLib from comtypes.gen, what next?

So your code should look like:

from comtypes.client import CreateObject

text="Hi there, how are you?"
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open('audio.mp3', SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()

This worked and saved a audio.mp3 file.

Upvotes: 2

Related Questions