Recently I had a requirement to synthesise speech from text on two different operating systems. Here is what I came up with.
OSX
Synthesising speech is a simple matter for OSX users because the operating system comes with the say command. We can use subprocess to call it.
1
2
3
4
5
6
|
import subprocess
def say(text):
subprocess.call(‘say ‘ + text, shell=True)
say(‘Hello, world!’)
|
Linux
On Linux, there are a few different options. I like to use the espeak Python bindings when I can. You can install it on Ubuntu using apt–get .
1
|
apt–get install python–espeak
|
Then use it like so:
1
2
|
from espeak import espeak
espeak.synth(‘Hello, world!’)
|
espeak supports multiple languages, so if you are not dealing with English text, you need to pass in the language code. Unfortunately, it looks like the Python bindings don’t support that yet, but we can still use subprocess like we did on linux.
1
2
3
4
|
import subprocess
def say_with_espeak(text, lang=“en”):
subprocess.call(“espeak -v {0} {1}”.format(lang, text), shell=True)
|