-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_to_text_handler.go
221 lines (186 loc) · 5.08 KB
/
speech_to_text_handler.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
package suzu
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"sync"
zlog "github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
)
func init() {
NewServiceHandlerFuncs.register("gcp", NewSpeechToTextHandler)
}
type SpeechToTextHandler struct {
Config Config
ChannelID string
ConnectionID string
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
func NewSpeechToTextHandler(config Config, channelID, connectionID string, sampleRate uint32, channelCount uint16, languageCode string, onResultFunc any) serviceHandlerInterface {
return &SpeechToTextHandler{
Config: config,
ChannelID: channelID,
ConnectionID: connectionID,
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
type GcpResult struct {
IsFinal *bool `json:"is_final,omitempty"`
Stability *float32 `json:"stability,omitempty"`
TranscriptionResult
}
func NewGcpResult() GcpResult {
return GcpResult{
TranscriptionResult: TranscriptionResult{
Type: "gcp",
},
}
}
func (gr *GcpResult) WithIsFinal(isFinal bool) *GcpResult {
gr.IsFinal = &isFinal
return gr
}
func (gr *GcpResult) WithStability(stability float32) *GcpResult {
gr.Stability = &stability
return gr
}
func (gr *GcpResult) SetMessage(message string) *GcpResult {
gr.Message = message
return gr
}
func (h *SpeechToTextHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
}
func (h *SpeechToTextHandler) GetRetryCount() int {
return h.RetryCount
}
func (h *SpeechToTextHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
}
func (h *SpeechToTextHandler) Handle(ctx context.Context, opusCh chan opusChannel, header soraHeader) (*io.PipeReader, error) {
stt := NewSpeechToText(h.Config, h.LanguageCode, int32(h.SampleRate), int32(h.ChannelCount))
packetReader, err := opus2ogg(ctx, opusCh, h.SampleRate, h.ChannelCount, h.Config, header)
if err != nil {
return nil, err
}
stream, err := stt.Start(ctx, packetReader)
if err != nil {
return nil, err
}
h.ResetRetryCount()
r, w := io.Pipe()
go func() {
encoder := json.NewEncoder(w)
for {
resp, err := stream.Recv()
if err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
if (strings.Contains(err.Error(), "code = OutOfRange")) ||
(strings.Contains(err.Error(), "code = InvalidArgument")) ||
(strings.Contains(err.Error(), "code = ResourceExhausted")) {
w.CloseWithError(ErrServerDisconnected)
return
}
if err := encoder.Encode(NewSuzuErrorResponse(err)); err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
}
w.CloseWithError(err)
return
}
if status := resp.Error; status != nil {
// 音声の長さの上限値に達した場合
err := fmt.Errorf("%s", status.GetMessage())
code := codes.Code(status.GetCode())
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Int32("code", status.GetCode()).
Send()
if code == codes.OutOfRange ||
code == codes.InvalidArgument ||
code == codes.ResourceExhausted {
err := errors.Join(err, ErrServerDisconnected)
w.CloseWithError(err)
return
}
w.CloseWithError(err)
return
}
if h.OnResultFunc != nil {
if err := h.OnResultFunc(ctx, w, h.ChannelID, h.ConnectionID, h.LanguageCode, resp.Results); err != nil {
if err := encoder.Encode(NewSuzuErrorResponse(err)); err != nil {
zlog.Error().
Err(err).
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Send()
}
w.CloseWithError(err)
return
}
} else {
for _, res := range resp.Results {
if stt.Config.FinalResultOnly {
if !res.IsFinal {
continue
}
}
result := NewGcpResult()
if stt.Config.GcpResultIsFinal {
result.WithIsFinal(res.IsFinal)
}
if stt.Config.GcpResultStability {
result.WithStability(res.Stability)
}
for _, alternative := range res.Alternatives {
if h.Config.GcpEnableWordConfidence {
for _, word := range alternative.Words {
zlog.Debug().
Str("channel_id", h.ChannelID).
Str("connection_id", h.ConnectionID).
Str("wrod", word.Word).
Float32("confidence", word.Confidence).
Str("start_time", word.StartTime.String()).
Str("end_time", word.EndTime.String()).
Send()
}
}
transcript := alternative.Transcript
result.SetMessage(transcript)
if err := encoder.Encode(result); err != nil {
w.CloseWithError(err)
return
}
}
}
}
}
}()
return r, nil
}