Text to speech (TTS) is the conversion of written text into spoken voice.You can create TTS programs in golang. 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 golang

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
package main

import (
"os/exec"
"log"
)

func main() {
s := "Make the computer speak"
cmd := exec.Command("espeak", s)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}

Example Google TTS with golang

Google speech synthesizes technology can also be used for text to speech programs. The example program below will convert text to speech using a Google API. You will need mplayer installed;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# needs mplayer installed
# apt install mplayer
#
# install htgo-tts
# go get "github.com/hegedustibor/htgo-tts"

package main

import "github.com/hegedustibor/htgo-tts"

func main() {
speech := htgotts.Speech{Folder: "audio", Language: "en"}
speech.Speak("Flying to the moon")
}

This will output spoken voice / an mp3 file.