Skip to content

Instantly share code, notes, and snippets.

@neguse
Created May 5, 2018 18:06
Show Gist options
  • Save neguse/b03c466519414b505811038438ac4a02 to your computer and use it in GitHub Desktop.
Save neguse/b03c466519414b505811038438ac4a02 to your computer and use it in GitHub Desktop.
Amazon Pollyを使って音声再生する
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/hajimehoshi/oto"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/polly"
)
func main() {
if len(os.Args) <= 1 {
fmt.Println("Usage: say <message to say>")
return
}
p, err := oto.NewPlayer(16000, 1, 2, 1000)
if err != nil {
return
}
defer p.Close()
svc := polly.New(session.New())
input := &polly.SynthesizeSpeechInput{
// LexiconNames: []*string{
// aws.String(""),
// },
OutputFormat: aws.String("pcm"),
SampleRate: aws.String("16000"),
Text: aws.String(os.Args[1]),
TextType: aws.String("text"),
VoiceId: aws.String("Mizuki"),
}
result, err := svc.SynthesizeSpeech(input)
if err != nil {
fmt.Println(err.Error())
return
}
stream, err := ioutil.ReadAll(result.AudioStream)
if err != nil {
fmt.Println(err.Error())
return
}
p.Write(stream)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment