forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcript.go
81 lines (70 loc) · 2.05 KB
/
transcript.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
// 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 transcript reads an audio file and outputs the transcript for it.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"golang.org/x/net/context"
"google.golang.org/api/option"
"google.golang.org/api/transport"
speech "google.golang.org/genproto/googleapis/cloud/speech/v1"
)
const usage = `Usage: transcript <audiofile>
Audio file is required to be 16-bit signed little-endian encoded
with a sample rate of 16000.
`
func main() {
flag.Parse()
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(2)
}
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()
c := speech.NewSpeechClient(conn)
// TODO(jbd): switch to the bidirectional streaming api
// and send data in small chunks.
data, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
// Send the contents of the audio file with the encoding and
// and sample rate information to be transcripted.
rresp, err := recognize(ctx, c, &data)
if err != nil {
log.Fatal(err)
}
// Print the results.
for _, resp := range rresp.Responses {
if resp.Error != nil {
fmt.Fprintf(os.Stderr, "error in recognize response: %v\n", resp.Error)
continue
}
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
}
}
}
}
func recognize(ctx context.Context, client speech.SpeechClient, data *[]byte) (*speech.NonStreamingRecognizeResponse, error) {
return client.NonStreamingRecognize(ctx, &speech.RecognizeRequest{
InitialRequest: &speech.InitialRecognizeRequest{
Encoding: speech.InitialRecognizeRequest_LINEAR16,
SampleRate: 16000,
},
AudioRequest: &speech.AudioRequest{Content: *data},
})
}