-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_to_text.go
180 lines (165 loc) · 6.01 KB
/
speech_to_text.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
package suzu
import (
"context"
"errors"
"io"
speech "cloud.google.com/go/speech/apiv1"
zlog "github.com/rs/zerolog/log"
"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/wrapperspb"
speechpb "cloud.google.com/go/speech/apiv1/speechpb"
)
type SpeechToText struct {
SampleReate int32
ChannelCount int32
LanguageCode string
Config Config
}
func NewSpeechToText(config Config, languageCode string, sampleRate, channelCount int32) SpeechToText {
return SpeechToText{
LanguageCode: languageCode,
SampleReate: sampleRate,
ChannelCount: channelCount,
Config: config,
}
}
func (stt SpeechToText) Start(ctx context.Context, r io.Reader) (speechpb.Speech_StreamingRecognizeClient, error) {
config := stt.Config
recognitionConfig := NewRecognitionConfig(config, stt.LanguageCode, int32(config.SampleRate), int32(config.ChannelCount))
speechpbRecognitionConfig := NewSpeechpbRecognitionConfig(recognitionConfig)
streamingRecognitionConfig := NewStreamingRecognitionConfig(speechpbRecognitionConfig, config.GcpSingleUtterance, config.GcpInterimResults)
var opts []option.ClientOption
credentialFile := config.GcpCredentialFile
if credentialFile != "" {
opts = append(opts, option.WithCredentialsFile(credentialFile))
}
client, err := speech.NewClient(ctx, opts...)
if err != nil {
return nil, &SuzuError{
// TODO: 適切な StatusCode に変更する
Code: 500,
Message: err.Error(),
}
}
stream, err := client.StreamingRecognize(ctx)
if err != nil {
return nil, &SuzuError{
// TODO: 適切な StatusCode に変更する
Code: 500,
Message: err.Error(),
}
}
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: streamingRecognitionConfig,
}); err != nil {
return nil, &SuzuError{
// TODO: 適切な StatusCode に変更する
Code: 500,
Message: err.Error(),
}
}
go func() {
defer stream.CloseSend()
for {
buf := make([]byte, FrameSize)
n, err := r.Read(buf)
if err != nil {
if errors.Is(err, io.EOF) {
// TODO: エラー処理
zlog.Info().Err(err).Send()
return
}
zlog.Error().Err(err).Send()
return
}
if n > 0 {
audioContent := buf[:n]
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: audioContent,
},
}); err != nil {
if errors.Is(err, io.EOF) {
// TODO: エラー処理
zlog.Info().Err(err).Send()
return
}
zlog.Error().Err(err).Send()
return
}
}
}
}()
return stream, nil
}
type RecognitionConfig struct {
Encoding speechpb.RecognitionConfig_AudioEncoding
SampleRateHertz int32
AudioChannelCount int32
EnableSeparateRecognitionPerChannel bool
LanguageCode string
AlternativeLanguageCodes []string
MaxAlternatives int32
ProfanityFilter bool
SpeechContexts []*speechpb.SpeechContext
EnableWordTimeOffsets bool
EnableWordConfidence bool
EnableAutomaticPunctuation bool
EnableSpokenPunctuation bool
EnableSpokenEmojis bool
Model string
UseEnhanced bool
}
func NewRecognitionConfig(c Config, languageCode string, sampleRate, channelCount int32) RecognitionConfig {
return RecognitionConfig{
Encoding: speechpb.RecognitionConfig_OGG_OPUS,
SampleRateHertz: sampleRate,
AudioChannelCount: channelCount,
EnableSeparateRecognitionPerChannel: c.GcpEnableSeparateRecognitionPerChannel,
LanguageCode: languageCode,
AlternativeLanguageCodes: c.GcpAlternativeLanguageCodes,
MaxAlternatives: c.GcpMaxAlternatives,
ProfanityFilter: c.GcpProfanityFilter,
SpeechContexts: []*speechpb.SpeechContext{
// &speechpb.SpeechContext{
// Phrases: []string{},
// },
},
EnableWordTimeOffsets: c.GcpEnableWordTimeOffsets,
EnableWordConfidence: c.GcpEnableWordConfidence,
EnableAutomaticPunctuation: c.GcpEnableAutomaticPunctuation,
EnableSpokenPunctuation: c.GcpEnableSpokenPunctuation,
EnableSpokenEmojis: c.GcpEnableSpokenEmojis,
Model: c.GcpModel,
UseEnhanced: c.GcpUseEnhanced,
}
}
func NewSpeechpbRecognitionConfig(rc RecognitionConfig) *speechpb.RecognitionConfig {
return &speechpb.RecognitionConfig{
Encoding: rc.Encoding,
SampleRateHertz: rc.SampleRateHertz,
AudioChannelCount: rc.AudioChannelCount,
EnableSeparateRecognitionPerChannel: rc.EnableSeparateRecognitionPerChannel,
LanguageCode: rc.LanguageCode,
AlternativeLanguageCodes: rc.AlternativeLanguageCodes,
MaxAlternatives: rc.MaxAlternatives,
ProfanityFilter: rc.ProfanityFilter,
SpeechContexts: rc.SpeechContexts,
EnableWordTimeOffsets: rc.EnableWordTimeOffsets,
EnableWordConfidence: rc.EnableWordConfidence,
EnableAutomaticPunctuation: rc.EnableAutomaticPunctuation,
EnableSpokenPunctuation: wrapperspb.Bool(rc.EnableSpokenPunctuation),
EnableSpokenEmojis: wrapperspb.Bool(rc.EnableSpokenEmojis),
Model: rc.Model,
UseEnhanced: rc.UseEnhanced,
}
}
func NewStreamingRecognitionConfig(recognitionConfig *speechpb.RecognitionConfig, singleUtterance, interimResults bool) *speechpb.StreamingRecognizeRequest_StreamingConfig {
return &speechpb.StreamingRecognizeRequest_StreamingConfig{
StreamingConfig: &speechpb.StreamingRecognitionConfig{
Config: recognitionConfig,
SingleUtterance: singleUtterance,
InterimResults: interimResults,
},
}
}