nateshmbhat / pyttsx3

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

Engine Hangs After Speaking #126

Open jsl303 opened 4 years ago

jsl303 commented 4 years ago

The following test hangs after speaking 0.

import pyttsx3

def speak(msg): engine.say(msg) engine.runAndWait()

engine = pyttsx3.init() for i in range(3): speak(str(i))

Mishalovoper commented 4 years ago

probably the method runAndWait() as it indicate it should run, speak then wait for few moments

jsl303 commented 4 years ago

@Mishalovoper, thanks for the suggestion, but it's the same even if I put time.sleep between call.

nateshmbhat commented 4 years ago

Which os are you using? Which version of pyttsx3 are you using?

jsl303 commented 4 years ago

Raspberry Pi 4: 2020-05-27-raspios-buster-full-armhf pyttsx3: 2.88 Bluealsa: 1.4.0

capital-G commented 3 years ago

For me it is stucks as well on OSX and it has to do with NSRunLoop of Foundation, but couldn't find much into it - it seems on the 2nd call it will never call stopper.stop

https://github.com/ronaldoussoren/pyobjc/blob/4f77802f2a71a2e7660ce5e53fe608d42aae0e26/pyobjc-framework-Cocoa/Lib/PyObjCTools/AppHelper.py#L262-L263

One dirty workaround is to reset the state of pyttsx3 by reimporting it on every run

import pyttsx3
import importlib

for words in words_to_say:
    importlib.reload(pyttsx3)
    engine = pyttsx3.init()
    engine.save_to_file(words, f'{words}.mp3'))
    engine.runAndWait()
Ajay-Singh-Rana commented 2 years ago

@capital-G Thanks dude...I searched for a whole day but i couldn't find a solution to it.Your answer helped me.But i have something more to ask too... After fixing that error i get another error:

Traceback (most recent call last):
  File "/home/bo/.local/lib/python3.9/site-packages/pyttsx3/drivers/espeak.py", line 171, in _onSynth
    self._proxy.notify('finished-utterance', completed=True)
ReferenceError: weakly-referenced object no longer exists

could you help with this if you ever faced this...Can't find a stackoverflow answer either.

ruckard commented 2 years ago

I had the same problem as @Ajay-Singh-Rana . My workaround uses an auxiliary function and that seems to finally do the trick.

import pyttsx3
import importlib

def engine_init ():
    importlib.reload(pyttsx3) # Workaround to be avoid pyttsx3 being stuck
    engine = pyttsx3.init()
    return engine

words_to_say = [ "one", "two", "three" ]

for words in words_to_say:
    importlib.reload(pyttsx3)
    engine = engine_init()
    engine.save_to_file(words, f'{words}.mp3')
    engine.runAndWait()
AnthonyLe93 commented 10 months ago

Hi @ruckard, your workaround works for me on MacOS X. I am unsure about the root cause of the issue so would be great if someone could chip in to explain?