Skip to content

#284: Basic Text-to-Speech With Google Translate

When it comes to text-to-speech (TTS), we may think that this is a solved problem, and we can use any library we find to get a good result. Unfortunately, that is not the case. There are a lot of older models that sound like a robot and even newer ones are far away from the quality we got used to by commercial products.

For our first steps with text-to-speech we try a minimal approach and use Google Translate. That gives us a quick win and is enough if we only want to work with a few sentences. The next posts will cover locally installed solutions that do not send data to a service.

Installation

We can install the package gTTS to access the Google TTS service with this uv command:

uv pip install gTTS

Turn text into speech

With this code snipped we can call to the Google Translate API, save the audio file as a temporary file and then play it with playsound3:

from gtts import gTTS, lang
import tempfile
from playsound3 import playsound

# example with an Enlgish text and an English voice
with tempfile.NamedTemporaryFile(mode='wb+', delete=True, suffix=".mp3") as tmp:
    text = "Hello, I am speaking using Google's text to speech service."

    tts = gTTS(text=text, lang='en')

    tts.write_to_fp(tmp)
    tmp.seek(0)
    # tts.save("output.mp3") # for regular files

    playsound(tmp.name)

Use different languages

To get a list of supported languages, we can use this code:

1
2
3
4
# supported languages
languages = lang.tts_langs()
for key in languages.keys():
    print(f"{key}: \t {languages[key]}")
af:      Afrikaans
am:      Amharic
ar:      Arabic
bg:      Bulgarian
bn:      Bengali
bs:      Bosnian
ca:      Catalan
cs:      Czech
cy:      Welsh
da:      Danish
de:      German
el:      Greek
en:      English
es:      Spanish
et:      Estonian
eu:      Basque
fi:      Finnish
fr:      French
fr-CA:   French (Canada)
gl:      Galician
gu:      Gujarati
ha:      Hausa
hi:      Hindi
hr:      Croatian
hu:      Hungarian
id:      Indonesian
is:      Icelandic
it:      Italian
iw:      Hebrew
ja:      Japanese
jw:      Javanese
km:      Khmer
kn:      Kannada
ko:      Korean
la:      Latin
lt:      Lithuanian
lv:      Latvian
ml:      Malayalam
mr:      Marathi
ms:      Malay
my:      Myanmar (Burmese)
ne:      Nepali
nl:      Dutch
no:      Norwegian
pa:      Punjabi (Gurmukhi)
pl:      Polish
pt:      Portuguese (Brazil)
pt-PT:   Portuguese (Portugal)
ro:      Romanian
ru:      Russian
si:      Sinhala
sk:      Slovak
sq:      Albanian
sr:      Serbian
su:      Sundanese
sv:      Swedish
sw:      Swahili
ta:      Tamil
te:      Telugu
th:      Thai
tl:      Filipino
tr:      Turkish
uk:      Ukrainian
ur:      Urdu
vi:      Vietnamese
yue:     Cantonese
zh-CN:   Chinese (Simplified)
zh-TW:   Chinese (Mandarin/Taiwan)
zh:      Chinese (Mandarin)

To use a different language than English, we can change the lang parameter and should make sure that the text we want to turn into an audio output matches the language we provide – otherwise the output will sound totally wrong:

# example with a German text and a German voice
with tempfile.NamedTemporaryFile(mode='wb+', delete=True, suffix=".mp3") as tmp:
    text = "Dies ist ein Text auf Deutsch."

    tts = gTTS(text=text, lang='de')

    tts.write_to_fp(tmp)
    tmp.seek(0)

    playsound(tmp.name)

Customisations

With gTTS we only have a small set of customisations we can choose from. The parameter slow gives us a slightly slower output, while the tld parameter let us choose a dialect. Unfortunately, not all supported dialects have an impact on the voice and there is no comprehensive list of what combinations we can use. As a starting point you can go with the list in the documentation.

 tts = gTTS(text=text, lang='en', slow=True, tld='com.ng')

Limitations

Aside from the lack of customisations, we have the problem of sending data to Google. While that is no problem for a small demo script, it may not be an acceptable approach for a business application. Not only may that violate the TOS agreement with Google, but it may put sensitive data at risk.

Next

While gTTS gives us a quick way to turn text into speech, I am not happy with this approach. It works, but the dependence on an external service is not optimal. Next week we explore pyttsx3 to get a first option to keep everything on our device.