-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunitystreamingcaption.go
More file actions
139 lines (126 loc) · 3.6 KB
/
unitystreamingcaption.go
File metadata and controls
139 lines (126 loc) · 3.6 KB
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
// unitygooglelivecaption.go
// intended to be used in unity project
// modification of livecaption.go found in https://github.com/GoogleCloudPlatform/golang-samples.git
//
// process requires google service accounts credential file's path
// you can get one in your google cloud console
// ----------------------------------------------------------------//
// livecaption.go
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command livecaption pipes the stdin audio data to
// Google Speech API and outputs the transcript.
package main
import (
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"os"
speech "cloud.google.com/go/speech/apiv1beta1"
"golang.org/x/net/context"
"google.golang.org/api/option"
"google.golang.org/api/transport"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1beta1"
)
var (
version = "v0.1"
responcePrefix = "response: "
)
func main() {
log.SetOutput(os.Stderr)
var credentialDir string
var language string
var singleUtteranceEnable bool
var outputAsBase64 bool
flag.StringVar(&credentialDir, "cred", "/path/to/file/", "path of google service account credential json file")
flag.StringVar(&language, "language", "ja-JP", "languagecode of voice")
flag.BoolVar(&singleUtteranceEnable, "s", false, "enable single utterance termination")
flag.BoolVar(&outputAsBase64, "base64", false, "output as base64 Encoding")
flag.Parse()
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", credentialDir)
ctx := context.Background()
conn, err := transport.DialGRPC(
ctx,
option.WithEndpoint("speech.googleapis.com:443"),
option.WithScopes("https://www.googleapis.com/auth/cloud-platform"),
)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// [START speech_streaming_mic_recognize]
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
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: &speechpb.StreamingRecognitionConfig{
Config: &speechpb.RecognitionConfig{
LanguageCode: language,
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRate: 16000,
},
InterimResults: true,
SingleUtterance: singleUtteranceEnable,
},
},
}); err != nil {
log.Fatal(err)
}
log.Println("Caption is ready")
go func() {
// Pipe stdin to the API.
buf := make([]byte, 1024)
for {
n, err := os.Stdin.Read(buf)
if err == io.EOF {
return // Nothing else to pipe, return from this goroutine.
}
if err != nil {
log.Printf("Could not read from stdin: %v", err)
continue
}
rqst := &speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: buf[:n],
},
}
if err = stream.Send(rqst); err != nil {
log.Printf("Could not send audio: %v", err)
}
}
}()
for {
resp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("Cannot stream results: %v", err)
}
if err := resp.Error; err != nil {
log.Fatalf("Could not recognize: %v", err)
}
for _, result := range resp.Results {
output := result.Alternatives[0].Transcript
if outputAsBase64 {
output = base64.StdEncoding.EncodeToString([]byte(output))
}
if result.IsFinal {
fmt.Println("EOS:" + output)
} else {
fmt.Println(output)
}
}
}
// [END speech_streaming_mic_recognize]
}