/
beepspeak.go
161 lines (130 loc) · 3.41 KB
/
beepspeak.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package beepspeak
// $ apt install libasound2-dev
import (
"bytes"
"context"
"fmt"
"os"
"strings"
"time"
texttospeech "cloud.google.com/go/texttospeech/apiv1"
"github.com/erikbryant/aes"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
texttospeechpb "cloud.google.com/go/texttospeech/apiv1/texttospeechpb"
)
// InitSay saves our GCP Speech API credentials to disk so that the
// GCP speech API can find them.
func InitSay(gcpAuthCrypt, passPhrase string) error {
path := os.TempDir() + "/" + "gcp_auth.json"
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
gcpAuth, err := aes.Decrypt(gcpAuthCrypt, passPhrase)
if err != nil {
return err
}
_, err = f.WriteString(gcpAuth + "\n")
if err != nil {
return err
}
// The Google API looks for the auth using an env var.
err = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", path)
if err != nil {
return err
}
return nil
}
// playStream plays a given audio stream.
func playStream(s beep.StreamSeekCloser, format beep.Format) {
// Init the Speaker with the SampleRate of the format and a buffer size.
speaker.Init(format.SampleRate, format.SampleRate.N(3*time.Second))
// Channel, which will signal the end of the playback.
playing := make(chan struct{})
// Now we Play our Streamer on the Speaker
speaker.Play(beep.Seq(s, beep.Callback(func() {
// Callback after the stream Ends
close(playing)
})))
<-playing
}
// Play plays a given sound file. MP3 and WAV are supported.
func Play(file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
var (
s beep.StreamSeekCloser
format beep.Format
)
if strings.HasSuffix(file, ".wav") {
s, format, err = wav.Decode(f)
if err != nil {
return err
}
} else {
s, format, err = mp3.Decode(f)
if err != nil {
return err
}
}
playStream(s, format)
return nil
}
// readable makes a string more human readable by removing some non alphanumeric
// and non-punctuation.
func readable(text string) string {
text = strings.TrimSpace(text)
text = strings.ReplaceAll(text, "_", " ")
text = strings.ReplaceAll(text, "/", " ")
text = strings.ReplaceAll(text, "[", " ")
text = strings.ReplaceAll(text, "]", " ")
text = strings.ReplaceAll(text, "\"", "")
text = strings.ReplaceAll(text, "^", "")
return text
}
// Say converts text to speech and then plays it.
func Say(text string) error {
_, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
if !ok {
return fmt.Errorf("ERROR: env var is not set; did you call InitSay()")
}
text = readable(text)
ctx := context.Background()
c, err := texttospeech.NewClient(ctx)
if err != nil {
return err
}
req := &texttospeechpb.SynthesizeSpeechRequest{
Input: &texttospeechpb.SynthesisInput{
InputSource: &texttospeechpb.SynthesisInput_Text{
Text: text,
},
},
Voice: &texttospeechpb.VoiceSelectionParams{
// https://cloud.google.com/text-to-speech/docs/voices
LanguageCode: "en-US",
Name: "en-US-Standard-C",
},
AudioConfig: &texttospeechpb.AudioConfig{
AudioEncoding: texttospeechpb.AudioEncoding_LINEAR16,
SpeakingRate: 1.1,
},
}
resp, err := c.SynthesizeSpeech(ctx, req)
if err != nil {
return err
}
r := bytes.NewReader(resp.GetAudioContent())
s, format, err := wav.Decode(r)
if err != nil {
return err
}
playStream(s, format)
return nil
}