nateshmbhat / pyttsx3

Offline Text To Speech synthesis for python
Mozilla Public License 2.0
2.12k stars 332 forks source link

the method-save_to_file on Windows Server 2019 raise exception _ctypes.COMError: (-2147200966, None, (None, None, None, 0, None)) #260

Open liudashang opened 1 year ago

liudashang commented 1 year ago

I'm writing a flask web which using the pyttsx3. the server's OS is win server 2019, it is a vmware virtualmachine. The virtualmachine does not have an audio card. When I using RDP(mstsc) to connect the server, server 2019 will create a audio output device by “Remote audio playback”. I login the web by myself computer, and create a tts job. The program writing a wav file is OK. But, when I disconnect the server, I create a tts job, the program writing a empty wav file. And the pyttsx3's debug info display:

Traceback (most recent call last):
  File "C:\Users\XXXXXX\AppData\Roaming\Python\Python39\site-packages\pyttsx3\driver.py", line 90, in _pump
    cmd[0](*cmd[1])
  File "C:\Users\XXXXXX\AppData\Roaming\Python\Python39\site-packages\pyttsx3\drivers\sapi5.py", line 69, in save_to_file
    temp_stream = self._tts.AudioOutputStream
_ctypes.COMError: (-2147200966, None, (None, None, None, 0, None))

The reason for this situation is, win server 2019 dose not have an audio output device, SpVoice AudioOutputStream Object will raise an exception.

My suggestion, change the source code pyttsx3\drivers\sapi5.py 'save_to_file'. add a 'try except' like this:

def save_to_file(self, text, filename):
    cwd = os.getcwd()
    stream = comtypes.client.CreateObject('SAPI.SPFileStream')
    stream.Open(filename, SpeechLib.SSFMCreateForWrite)
    try:
        temp_stream = self._tts.AudioOutputStream
        self._tts.AudioOutputStream = stream
        self._tts.Speak(fromUtf8(toUtf8(text)))
        self._tts.AudioOutputStream = temp_stream
    except:
        self._tts.AudioOutputStream = stream
        self._tts.Speak(fromUtf8(toUtf8(text)))
    stream.close()
    os.chdir(cwd)
Jiangshan00001 commented 1 year ago

i have changed the code and commit to:https://github.com/Jiangshan00001/pyttsx4 you can have a try if you need:

pip install pyttsx4
import pyttsx4
engine = pyttsx4.init()
engine.save_to_file('Hello World' , 'test.wav')
engine.runAndWait()
grydev commented 1 year ago

My suggestion, change the source code pyttsx3\drivers\sapi5.py 'save_to_file'. add a 'try except' like this

Adding the try except works perfect on a windows virtual machine - thank you!

willwade commented 1 week ago

Pretty sure we’ve fixed this. Will double check.