Convert Text to Speech with AWS Polly

package main

import (
    "io/ioutil"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/polly"

    "fmt"
    "io"
    "os"
    "strings"
)

func main() {
    if len(os.Args) < 2 {
        fmt.Println("Please supply")
        os.Exit(1)
    }

    // The name of the text file to convert to MP3
    fileName := os.Args[1]

    // Open text file and get it's contents as a string
    contents, err := ioutil.ReadFile(fileName)
    if err != nil {
        fmt.Println("Got error opening file " + fileName)
        fmt.Print(err.Error())
        os.Exit(1)
    }

    // // Convert bytes to string
    s := string(contents[:])

    // Initialize a session that the SDK uses to load
    // credentials from the shared credentials file. (~/.aws/credentials).
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    // Create Polly client
    svc := polly.New(sess)

    // Output to MP3 using voice Joanna
    input := &polly.SynthesizeSpeechInput{
        OutputFormat: aws.String("mp3"),
        Text:         aws.String(s),
        VoiceId:      aws.String("Joanna"),
    }

    output, err := svc.SynthesizeSpeech(input)
    if err != nil {
        fmt.Println("Got error calling SynthesizeSpeech:")
        fmt.Print(err.Error())
        os.Exit(1)
    }

    // Save as MP3
    names := strings.Split(fileName, ".")
    name := names[0]
    mp3File := name + ".mp3"

    outFile, err := os.Create(mp3File)
    if err != nil {
        fmt.Println("Got error creating " + mp3File + ":")
        fmt.Print(err.Error())
        os.Exit(1)
    }

    defer outFile.Close()
    _, err = io.Copy(outFile, output.AudioStream)
    if err != nil {
        fmt.Println("Got error saving MP3:")
        fmt.Print(err.Error())
        os.Exit(1)
    }
}

Save the file as polly_example.go. Then, open the command line and build the program. We're going to name our compiled program simply polly via the -o flag.

$ go build -o polly polly_example.go

We need some text input for our program. Create a new file (via any text editor) called parrot.txt in the same folder as our program executable. In that file, type Polly want a cracker? and save the file.

Finally, let's return to the command line and run our polly program, passing it the text file we just created.

$ ./polly parrot.txt

Once that process finishes, try playing the newly create file parrot.mp3 in your favorte media player.