Text to speech (TTS) is the conversion of written text into spoken voice.You can create TTS programs in ruby. The quality of the spoken voice depends on your speech engine.

In this article you’ll learn how to create your own TTS program.

Text to speech in ruby

Example with espeak

The program ‘espeak’ is a simple speech synthesizer which converst written text into spoken voice. The espeak program does sound a bit robotic, but its simple enough to build a basic program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/ruby

# Setup ruby gem
# gem install espeak-ruby
#
# Get the programs espeak and lame:
# apt install espeak
# apt install lame

require 'espeak'

include ESpeak
speech = Speech.new("Hello speak!", voice: "en")
speech.speak
speech.save("hello.mp3")

Example TTS with Google

You can use the Google TTS service as speech engine. This generates either an mp3 on your disk, or you can play it directly from within your Ruby program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# gem install tts
# apt install mpg123

require 'tts'

# Will download "Ruby!.mp3" to your current directory
# Supported languages: ["zh", "en", "it", "fr"]
#"Ruby!".to_file "en"

# Playback in English (1 times)

s = "Say something in English"
repeatTimes = 1
s.play("en", repeatTimes)

This will output spoken voice / an mp3 file.