Audio can be a bit dismal on a Raspberry Pi. Once you get a configuration that works, sometimes you’re not sure how you got there and you’ll do anything to keep that arcane setup going. It’s better than it was.
Speech synthesis or TTS adds an extra layer for potential failure. One of the popular Linux TTS systems, eSpeak, hasn’t seen much development in almost a decade and seems to only work through workarounds and hand-waving.
Thankfully, there’s a fork of eSpeak that is maintained: espeak-ng. Better yet, it’s packaged with Raspberry Pi OS and can be installed quite easily:
sudo apt install espeak-ng espeak-ng-data libespeak-ng-dev
In my simple tests, it output everything I expected of it.
eSpeak had a Python module that kinda worked, but espeak-ng’s is much more ambitious, and (mostly) does what it sets out to do. You can install it like this:
sudo pip3 install py-espeak-ng
py-espeak-ng has some documentation, but it’s still got some trial and error in getting it to work. The biggest issue that held me up was that the module needs to be initialized with a voice that espeak-ng already knows about. If you don’t specify a voice, or specify one that the system doesn’t know about, you won’t get any errors â€” but you won’t get any output, either.
Here’s a small Python example that you’ll probably want to try with no-one else within earshot. It repeats the same English phrase (a favourite of elocution teachers) in every English regional language that espeak-ng knows about. In addition, since I’m a dictionary nerd, it outputs phonetics too.
#!/usr/bin/python3 # -*- coding: utf-8 -*- # an espeakng elocution lesson from scruss, 2020-07 # I suffered this at school, now you get to as well! # You will need to: # sudo apt install espeak-ng espeak-ng-data libespeak-ng-dev # sudo pip3 install py-espeak-ng from espeakng import ESpeakNG from time import sleep # you have to initialize with a voice that exists # `espeak-ng --voices=en` will list English ones esng = ESpeakNG(voice='en-gb') esng.pitch = 32 esng.speed = 150 phrase = "Father's car is a Jaguar and pa drives rather fast. "\ "Castles, farms and draughty barns, all go charging past." print(phrase) print() for voice in esng.voices: if voice['language'].startswith('en-'): print('Using voice:', voice['language'], 'for', voice['voice_name'], '-') esng.voice = voice['language'] ipa = esng.g2p(phrase, ipa=2) print(voice['language'], 'phonetics:', ipa) esng.say(phrase, sync=True) print() sleep(0.1)
Be thankful you can’t hear the output. The IPA output, however, is a thing of beauty:
./espeakNG_test.py Father's car is a Jaguar and pa drives rather fast. Castles, farms and draughty barns, all go charging past. Using voice: en-029 for English_(Caribbean) - en-029 phonetics: fËˆÉ‘Ëdaz kËˆÉ‘Í¡É™É¹ Éªz a dÍ¡Ê’ËˆaÉ¡wÉ‘Í¡É™ and pËˆÉ‘Ë dÉ¹ËˆaÍ¡Éªvz É¹ËˆÉ‘Ëda fËˆaÍ¡astkËˆaÍ¡asÉ›lzfËˆÉ‘Í¡É™mz and dÉ¹ËˆaÍ¡afti bËˆÉ‘Í¡É™nzËˆÉ”Ël É¡ËŒoÍ¡ÊŠ tÍ¡ÊƒËˆÉ‘Í¡É™dÍ¡Ê’Éªn pËˆaÍ¡ast Using voice: en-gb for English_(Great_Britain) - en-gb phonetics: fËˆÉ‘ËÃ°É™z kËˆÉ‘ËÉ¹ Éªz É dÍ¡Ê’ËˆaÉ¡wÉ‘Ë and pËˆÉ‘Ë dÉ¹ËˆaÍ¡Éªvz É¹ËˆÉ‘ËÃ°É™ fËˆastkËˆasÉ™Í¡lzfËˆÉ‘Ëmz and dÉ¹Ëˆafti bËˆÉ‘ËnzËˆÉ”Ël É¡ËŒÉ™Í¡ÊŠ tÍ¡ÊƒËˆÉ‘ËdÍ¡Ê’ÉªÅ‹ pËˆast Using voice: en-gb-scotland for English_(Scotland) - en-gb-scotland phonetics: fËˆa:Ã°Éœz kËˆaËr Éªz É dÍ¡Ê’ËˆaÉ¡waËr and pËˆa: drËˆaÍ¡Éªvz rËˆa:Ã°Éœ fËˆa:stkËˆa:sÉ™Í¡lzfËˆaËrmz and drËˆa:fte bËˆaËrnzËˆÉ”Ël É¡ËŒoË tÍ¡ÊƒËˆaËrdÍ¡Ê’ÉªÅ‹ pËˆa:st â€¦