Archive

Posts Tagged ‘read text’

text to speech

October 26, 2020 Leave a comment

Problem #1

You have a text and you want to convert it to audio (possibly to .mp3).

Solution #1

The gTTS module can do this for you.

gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Writes spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout. It features flexible pre-processing and tokenizing, as well as automatic retrieval of supported languages.” (source)

Example:

from gtts import gTTS

TEXT = """
Python is an interpreted, high-level and general-purpose programming
language. Created by Guido van Rossum and first released in 1991, Python's
design philosophy emphasizes code readability with its notable use of
significant whitespace. Its language constructs and object-oriented
approach aim to help programmers write clear, logical code for small
and large-scale projects.
""".replace("\n", " ")


def main():
    tts = gTTS(text=TEXT, lang='en')
    tts.save("audio.mp3")
    print("done")

The text is read by a pleasant female voice in good quality.

The reader takes a little pause at every end of line, that’s why newline characters are replaced by a space. This way the reading is fluid.

Problem #2

There is one little problem. I find the reading speed in audio.mp3 a bit slow. When you watch a YouTube video, there you have the possibility to speed up the audio by 25%, 50%, etc. How to play back audio.mp3 a bit faster?

Solution #2

$ play audio.mp3 tempo 1.15

or

$ mplayer audio.mp3 -speed 1.15 -af scaletempo

It means 15% faster playback.

gTTS from the command-line

If you install the package gtts, you also get a command-line program called gtts-cli. Some examples:

$ gtts-cli --all

^^^ List all languages. At the time of writing, the list contains 78 entries. (Hungarian is also there).

$ gtts-cli 'hello' | play -t mp3 -

^^^ Play the sound directly.

$ gtts-cli "bonjour tout le monde" --lang fr | play -t mp3 -

^^^ Specify the language of the text.

$ gtts-cli "c'est la vie" --lang fr --output cestlavie.mp3

^^^ Save the sound in an .mp3 file.

Links

Notes

I also tried the pyttsx library under Linux but the quality was terrible. It calls the command espeak and it reads the text like a f* robot. No, thanks. Maybe it sounds better under Windows (under Windows there is a different text to speech engine); I didn’t try that.

Categories: python Tags: , , ,
Design a site like this with WordPress.com
Get started