3

I'm using pyttsx in my game, but I have encountered a problem - method runAndWait() causes it to stop for a brief period of time to say the queued text. It is a problem, because it messes up my time counting. Is it possible to say a text but without stopping all other activities? Or maybe is there any other text-to-speech converter in python/pygame?

def say(text):
   voices = engine.getProperty('voices')
   engine.setProperty('voice', voices[1].id)
   engine.setProperty('rate', 250)
   engine.say(text)
   engine.runAndWait()
Hamman Samuel
  • 2,350
  • 4
  • 30
  • 41
  • 1
    it seems it runs event loop to work correctly - so you may have to run it in thread to not block your mainloop (event lop). It has also `startLoop()` and `endLoop()` to work with other event loop so maybe it can help - see [last example](http://pyttsx.readthedocs.io/en/latest/engine.html#using-an-external-event-loop). You whould have to put your mainloop between `startLoop` and `endLoop` and use `say` (and engine.iterate()`) inside both loops. – furas Dec 27 '17 at 22:11

2 Answers2

0

Pyttsx does not have method like runAndNoWait(), but you can run your say(text) function as separate thread to stop blocking your main game thread.

import threading
text = 'Hello world'
threading.Thread(target=say, args=(text,)).start()
MST
  • 651
  • 1
  • 4
  • 6
-1

You could always use espeak. Espeak does not have as clear of a sound a pyttsx, but it should work. Espeak comes installed with pyttsx, and to test it out, go to your terminal and type:

espeak "Hello!"

This should make the computer say "Hello!". In the case that you get an error regarding the fact that espeak is not a found command, install it by typing:

sudo apt-get install espeak

To access this with python here is a code using the os module:

import os
text = "Hi!"
os.system("espeak " + text)
xilpex
  • 3,097
  • 2
  • 14
  • 45