forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordoffset.go
145 lines (123 loc) · 3.52 KB
/
wordoffset.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
// 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 wordoffset sends audio data to the Google Speech API
// and prints word offset information.
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
speech "cloud.google.com/go/speech/apiv1"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
)
const usage = `Usage: wordoffset <audiofile>
Audio file must be a 16-bit signed little-endian encoded
with a sample rate of 16000.
The path to the audio file may be a GCS URI (gs://...).
`
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(2)
}
var sendFunc func(*speech.Client, io.Writer, string) error
path := os.Args[1]
if strings.Contains(path, "://") {
sendFunc = asyncWords
} else {
sendFunc = syncWords
}
ctx := context.Background()
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
if err := sendFunc(client, os.Stdout, os.Args[1]); err != nil {
log.Fatal(err)
}
}
// [START speech_transcribe_async_word_time_offsets_gcs]
func asyncWords(client *speech.Client, out io.Writer, gcsURI string) error {
ctx := context.Background()
// Send the contents of the audio file with the encoding and
// and sample rate information to be transcripted.
req := &speechpb.LongRunningRecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "en-US",
EnableWordTimeOffsets: true,
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Uri{Uri: gcsURI},
},
}
op, err := client.LongRunningRecognize(ctx, req)
if err != nil {
return err
}
resp, err := op.Wait(ctx)
if err != nil {
return err
}
// Print the results.
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
fmt.Fprintf(out, "\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
for _, w := range alt.Words {
fmt.Fprintf(out,
"Word: \"%v\" (startTime=%3f, endTime=%3f)\n",
w.Word,
float64(w.StartTime.Seconds)+float64(w.StartTime.Nanos)*1e-9,
float64(w.EndTime.Seconds)+float64(w.EndTime.Nanos)*1e-9,
)
}
}
}
return nil
}
// [END speech_transcribe_async_word_time_offsets_gcs]
func syncWords(client *speech.Client, out io.Writer, file string) error {
ctx := context.Background()
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
// Send the contents of the audio file with the encoding and
// and sample rate information to be transcripted.
req := &speechpb.RecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "en-US",
EnableWordTimeOffsets: true,
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Content{Content: data},
},
}
resp, err := client.Recognize(ctx, req)
if err != nil {
return err
}
// Print the results.
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
fmt.Fprintf(out, "\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
for _, w := range alt.Words {
fmt.Fprintf(out,
"Word: \"%v\" (startTime=%3f, endTime=%3f)\n",
w.Word,
float64(w.StartTime.Seconds)+float64(w.StartTime.Nanos)*1e-9,
float64(w.EndTime.Seconds)+float64(w.EndTime.Nanos)*1e-9,
)
}
}
}
return nil
}