Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/polly"
  7. "github.com/chzyer/readline"
  8. "io/ioutil"
  9. "os"
  10. "os/exec"
  11. )
  12.  
  13. func main() {
  14. // Create readline instance
  15. l, err := readline.New(" > ")
  16. if err != nil {
  17. panic(err)
  18. }
  19. defer l.Close()
  20.  
  21. // Create amazon polly client
  22. sess, err := session.NewSessionWithOptions(session.Options{
  23. Config: aws.Config{
  24. Region: aws.String("eu-central-1"),
  25. },
  26. Profile: "amazon-polly-profile",
  27. SharedConfigState: session.SharedConfigEnable,
  28. })
  29. if err != nil {
  30. panic(err)
  31. }
  32. client := polly.New(sess)
  33.  
  34. // Do forever
  35. for {
  36. // Read a line from the user
  37. line, err := l.Readline()
  38. if err != nil {
  39. panic(err)
  40. }
  41. // Use amazon polly to synthesize speach
  42. input := &polly.SynthesizeSpeechInput{
  43. OutputFormat: aws.String("ogg_vorbis"),
  44. Text: aws.String(line),
  45. VoiceId: aws.String("Marlene"),
  46. }
  47. result, err := client.SynthesizeSpeech(input)
  48.  
  49. if err != nil {
  50. panic(err)
  51. }
  52. defer result.AudioStream.Close()
  53.  
  54. // Write the resulting ogg to /tmp/test.ogg
  55. bytes, _ := ioutil.ReadAll(result.AudioStream)
  56. ioutil.WriteFile("/tmp/test.ogg", bytes, os.ModePerm)
  57.  
  58. // Play the ogg using mplayer
  59. cmd := exec.Command("mplayer", "/tmp/test.ogg")
  60. cmd.Run()
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement