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.


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;


# 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.