Realistic text to speech with Python that doesn't require internet?

Edgecase picture Edgecase · Jan 25, 2018 · Viewed 32.2k times · Source

I'm trying to create an artificially intelligent program (nothing really big or special) and I wanted it to have a voice (who wouldn't?). I've looked into espeak, festival, gTTS and they're nice and usable, but not realistic enough for me to really be proud of, if that makes sense. I've been looking for something more realistic. Like this

from gtts import gTTS

tts = gTTS(text='what to say', lang='en')
tts.save('/path/to/file.mp3')

gTTS works fine. I love it. It's realistic, but it requires Internet.. The issue is, I want my application to be as independent as possible. And I hate depending on Internet access.

Are there any other options?

PS: I'm currently running Linux, so your OS might have a different solution.

Answer

Isma picture Isma · Jan 25, 2018

Try to use pyttsx3 2.5, according the documentation:

gTTS which works perfectly in python3 but it needs internet connection to work since it relies on google to get the audio data.But Pyttsx is completely offline and works seemlesly and has multiple tts-engine support.

Works for Python 2 and 3

To install it:

pip install pyttsx3

Using it should be as simple as:

import pyttsx3;
engine = pyttsx3.init();
engine.say("I will speak this text");
engine.runAndWait() ;

Edit 1 - Changing the voice

To get a less robotic voice you can try to change the voice as follows:

engine.setProperty('voice', voice.id)

To get the available voices

voices = engine.getProperty('voices')

You can try the different available voices as explained in this question: Changing the voice with PYTTSX module in python.

Edit 2 - Selecting speech engine

The library supports the following engines:

  • sapi5 - SAPI5 on Windows
  • nsss - NSSpeechSynthesizer on Mac OS X
  • espeak - eSpeak on every other platform

If espeak is not very natural you can try sapi5 if you are on Windows or nsss if you are on Mac OS X.

You can specify the engine in the init method, e.g.:

pyttsx3.init(driverName='sapi5') 

More info here: http://pyttsx3.readthedocs.io/en/latest/engine.html