-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscriber.go
265 lines (238 loc) · 7.26 KB
/
transcriber.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"time"
speech "cloud.google.com/go/speech/apiv1p1beta1"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1p1beta1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
flushTimeout = flag.Duration("flushTimeout", 5*time.Second, "Emit any pending transcriptions after this time")
pendingWordCount = flag.Int("pendingWordCount", 5, "Treat last N transcribed words as pending")
sampleRate = flag.Int("sampleRate", 16000, "Sample rate (Hz)")
channels = flag.Int("channels", 1, "Number of audio channels")
lang = flag.String("lang", "en-US", "Transcription language code")
phrases = flag.String("phrases", "'some','english','hint'", "Comma-separated list of phrase hints for Speech API.")
logFile = flag.String("logFile", "out.log", "Log file ")
latestTranscript string
lastIndex = 0
pending []string
)
func main() {
flag.Parse()
file := initLogFile(*logFile)
defer file.Close()
speechClient, err := speech.NewClient(context.Background())
if err != nil {
log.Fatal(err)
}
contextPhrases := []string{}
if *phrases != "" {
contextPhrases = strings.Split(*phrases, ",")
log.Printf("Supplying %d phrase hints: %+q", len(contextPhrases), contextPhrases)
}
streamingConfig := speechpb.StreamingRecognitionConfig{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: int32(*sampleRate),
AudioChannelCount: int32(*channels),
LanguageCode: *lang,
EnableAutomaticPunctuation: true,
SpeechContexts: []*speechpb.SpeechContext{
{Phrases: contextPhrases},
},
},
InterimResults: true,
}
sendAudio(context.Background(), speechClient, streamingConfig)
}
// Consumes Stdin audio data and sends to Speech.
func sendAudio(ctx context.Context, speechClient *speech.Client, config speechpb.StreamingRecognitionConfig) {
var stream speechpb.Speech_StreamingRecognizeClient
receiveChan := make(chan bool)
for {
select {
case <-ctx.Done():
log.Printf("Context cancelled, exiting sender loop")
return
case _, ok := <-receiveChan:
if !ok && stream != nil {
log.Printf("Receive channel closed, resetting stream")
stream = nil
receiveChan = make(chan bool)
resetIndex()
continue
}
default:
// Process audio
}
// Establish bi-directional connection to Cloud Speech and
// start a go routine to listen for responses
if stream == nil {
stream = initStreamingRequest(ctx, speechClient, config)
go receiveResponses(stream, receiveChan)
log.Printf("[NEW STREAM]")
}
// Pipe stdin to the API.
buf := make([]byte, 1024)
n, err := os.Stdin.Read(buf)
if err != nil {
log.Printf("Could not read from stdin: %v", err)
continue
}
if n > 0 {
// Send audio, transcription responses received asynchronously
sendErr := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: buf[:n],
},
})
if sendErr != nil {
// Expected - if stream has been closed (e.g. timeout)
if sendErr == io.EOF {
continue
}
log.Printf("Could not send audio: %v", sendErr)
}
}
} // for
}
// Consumes StreamingResponses from Speech.
// call stream.Recv()
func receiveResponses(stream speechpb.Speech_StreamingRecognizeClient, receiveChan chan bool) {
// Indicate that we're no longer listening for responses
defer close(receiveChan)
// If no results received from Speech for some period, emit any pending transcriptions
timer := time.NewTimer(*flushTimeout)
go func() {
<-timer.C
flush()
}()
defer timer.Stop()
// Consume streaming responses from Speech
for {
resp, err := stream.Recv()
if err != nil {
// Context cancelled - expected
if status.Code(err) == codes.Canceled {
return
}
log.Printf("Cannot stream results: %v", err)
return
}
if err := resp.Error; err != nil {
// Timeout - expected, when no audio sent for a time
// or The current maximum duration (streamingLimit) ~5 minutes is reached
if status.FromProto(err).Code() == codes.OutOfRange {
log.Printf("Timeout from API; closing connection")
return
}
log.Printf("Could not recognize: %v", err)
return
}
// If nothing received for a time, stop receiving
if !timer.Stop() {
return
}
// Ok, we have a valid response from Speech.
timer.Reset(*flushTimeout)
processResponses(*resp)
}
}
// Handles transcription results.
func processResponses(resp speechpb.StreamingRecognizeResponse) {
if len(resp.Results) == 0 {
return
}
result := resp.Results[0]
alternative := result.Alternatives[0]
latestTranscript = alternative.Transcript
elements := strings.Split(alternative.Transcript, " ")
length := len(elements)
// Speech will not further update this transcription; output it
if result.GetIsFinal() || alternative.GetConfidence() > 0 {
final := elements[lastIndex:]
emit(strings.Join(final, " "))
resetIndex()
return
}
// Unstable, speculative transcriptions (very likley to change)
unstable := ""
if len(resp.Results) > 1 {
unstable = resp.Results[1].Alternatives[0].Transcript
}
// Treat last N words as pending.
// Treat delta between last and current index as steady
if length < *pendingWordCount {
lastIndex = 0
pending = elements
emitStages([]string{}, pending, unstable)
} else if lastIndex < length-*pendingWordCount {
steady := elements[lastIndex:(length - *pendingWordCount)]
lastIndex += len(steady)
pending = elements[lastIndex:]
emitStages(steady, pending, unstable)
}
}
// initStreamingRequest Send the initial configuration message
// https://pkg.go.dev/cloud.google.com/[email protected]/speech/apiv1p1beta1?tab=doc#Client.StreamingRecognize
func initStreamingRequest(ctx context.Context, client *speech.Client, config speechpb.StreamingRecognitionConfig) speechpb.Speech_StreamingRecognizeClient {
stream, err := client.StreamingRecognize(ctx)
if err != nil {
log.Fatal(err)
}
// Send the initial configuration message.
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_StreamingConfig{
StreamingConfig: &config,
},
}); err != nil {
log.Printf("Error sending initial config message: %v", err)
return nil
}
log.Printf("Initialised new connection to Speech API")
return stream
}
func initLogFile(logFile string) *os.File {
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file", logFile, ":", err)
}
log.SetOutput(file)
log.Printf("Set logfile %s", logFile)
return file
}
func emitStages(steady []string, pending []string, unstable string) {
// Concatenate the different segments
// msg := fmt.Sprintf("%s|%s|%s", strings.Join(steady, " "),
// strings.Join(pending, " "), unstable)
msg := fmt.Sprintf("%s", strings.Join(steady, " ")+" ")
emit(msg)
}
func emit(msg string) {
fmt.Print(msg)
}
func flush() {
// log.Printf("flush()...")
msg := ""
if pending != nil {
msg += strings.Join(pending, " ")
}
if msg != "" {
log.Printf("Flushing...")
emit(msg)
}
resetIndex()
}
func resetIndex() {
// log.Printf("resetIndex()...")
lastIndex = 0
pending = nil
}