#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:
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:
Use different languages
To get a list of supported languages, we can use this code:
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:
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.
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.