/
sr.go
309 lines (256 loc) · 7.01 KB
/
sr.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
sr.go
Copyright 1999-present Alibaba Group Holding Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nls
import (
"encoding/json"
"errors"
"log"
"sync"
)
const (
//namespace field
SR_NAMESPACE = "SpeechRecognizer"
//name field
SR_START_NAME = "StartRecognition"
SR_STOP_NAME = "StopRecognition"
SR_STARTED_NAME = "RecognitionStarted"
SR_RESULT_CHG_NAME = "RecognitionResultChanged"
SR_COMPLETED_NAME = "RecognitionCompleted"
)
type SpeechRecognitionStartParam struct {
Format string `json:"format,omitempty"`
SampleRate int `json:"sample_rate,omitempty"`
EnableIntermediateResult bool `json:"enable_intermediate_result"`
EnablePunctuationPrediction bool `json:"enable_punctuation_prediction"`
EnableInverseTextNormalization bool `json:"enable_inverse_text_normalization"`
}
func DefaultSpeechRecognitionParam() SpeechRecognitionStartParam {
return SpeechRecognitionStartParam{
Format: "pcm",
SampleRate: 16000,
EnableIntermediateResult: true,
EnablePunctuationPrediction: true,
EnableInverseTextNormalization: true,
}
}
type SpeechRecognition struct {
nls *nlsProto
taskId string
startCh chan bool
stopCh chan bool
lk sync.Mutex
onTaskFailed func(text string, param interface{})
onStarted func(text string, param interface{})
onResultChanged func(text string, param interface{})
onCompleted func(text string, param interface{})
onClose func(param interface{})
StartParam map[string]interface{}
UserParam interface{}
}
func checkSrNlsProto(proto *nlsProto) *SpeechRecognition {
if proto == nil {
log.Default().Fatal("empty proto check failed")
return nil
}
sr, ok := proto.param.(*SpeechRecognition)
if !ok {
log.Default().Fatal("proto param not SpeechRecognition instance")
return nil
}
return sr
}
func onSrTaskFailedHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
if sr.onTaskFailed != nil {
sr.onTaskFailed(string(text), sr.UserParam)
}
sr.lk.Lock()
defer sr.lk.Unlock()
if sr.startCh != nil {
sr.startCh <- false
close(sr.startCh)
sr.startCh = nil
}
if sr.stopCh != nil {
sr.stopCh <- false
close(sr.stopCh)
sr.stopCh = nil
}
}
func onSrConnectedHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
req := CommonRequest{}
req.Context = DefaultContext
req.Header.Appkey = sr.nls.connConfig.Appkey
req.Header.MessageId = getUuid()
req.Header.Name = SR_START_NAME
req.Header.Namespace = SR_NAMESPACE
req.Header.TaskId = sr.taskId
req.Payload = sr.StartParam
b, _ := json.Marshal(req)
sr.nls.logger.Println("send:", string(b))
sr.nls.cmd(string(b))
}
func onSrCloseHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
if sr.onClose != nil {
sr.onClose(sr.UserParam)
}
sr.nls.shutdown()
}
func onSrStartedHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
if sr.onStarted != nil {
sr.onStarted(string(text), sr.UserParam)
}
sr.lk.Lock()
defer sr.lk.Unlock()
if sr.startCh != nil {
sr.startCh <- true
close(sr.startCh)
sr.startCh = nil
}
}
func onSrResultChangedHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
if sr.onResultChanged != nil {
sr.onResultChanged(string(text), sr.UserParam)
}
}
func onSrCompletedHandler(isErr bool, text []byte, proto *nlsProto) {
sr := checkSrNlsProto(proto)
if sr.onCompleted != nil {
sr.onCompleted(string(text), sr.UserParam)
}
sr.lk.Lock()
defer sr.lk.Unlock()
if sr.stopCh != nil {
sr.stopCh <- true
close(sr.stopCh)
sr.stopCh = nil
}
}
var srProto = commonProto{
namespace: SR_NAMESPACE,
handlers: map[string]func(bool, []byte, *nlsProto){
CLOSE_HANDLER: onSrCloseHandler,
CONNECTED_HANDLER: onSrConnectedHandler,
SR_STARTED_NAME: onSrStartedHandler,
SR_RESULT_CHG_NAME: onSrResultChangedHandler,
SR_COMPLETED_NAME: onSrCompletedHandler,
TASK_FAILED_NAME: onSrTaskFailedHandler,
},
}
func newSpeechRecognitionProto() *commonProto {
return &srProto
}
func NewSpeechRecognition(config *ConnectionConfig,
logger *NlsLogger,
taskfailed func(string, interface{}),
started func(string, interface{}),
resultchanged func(string, interface{}),
completed func(string, interface{}),
closed func(interface{}),
param interface{}) (*SpeechRecognition, error) {
sr := new(SpeechRecognition)
proto := newSpeechRecognitionProto()
if logger == nil {
logger = DefaultNlsLog()
}
nls, err := newNlsProto(config, proto, logger, sr)
if err != nil {
return nil, err
}
sr.nls = nls
sr.UserParam = param
sr.onTaskFailed = taskfailed
sr.onStarted = started
sr.onResultChanged = resultchanged
sr.onCompleted = completed
sr.onClose = closed
return sr, nil
}
func (sr *SpeechRecognition) Start(param SpeechRecognitionStartParam, extra map[string]interface{}) (chan bool, error) {
if sr.nls == nil {
return nil, errors.New("empty nls: using NewSpeechRecognition to create a valid instance")
}
b, err := json.Marshal(param)
if err != nil {
return nil, err
}
json.Unmarshal(b, &sr.StartParam)
if extra != nil {
if sr.StartParam == nil {
sr.StartParam = extra
} else {
for k, v := range extra {
sr.StartParam[k] = v
}
}
}
sr.taskId = getUuid()
err = sr.nls.Connect()
if err != nil {
return nil, err
}
sr.lk.Lock()
defer sr.lk.Unlock()
sr.startCh = make(chan bool, 1)
return sr.startCh, nil
}
func (sr *SpeechRecognition) Stop() (chan bool, error) {
if sr.nls == nil {
return nil, errors.New("empty nls: using NewSpeechRecognition to create a valid instance")
}
req := CommonRequest{}
req.Context = DefaultContext
req.Header.Appkey = sr.nls.connConfig.Appkey
req.Header.MessageId = getUuid()
req.Header.Name = SR_STOP_NAME
req.Header.Namespace = SR_NAMESPACE
req.Header.TaskId = sr.taskId
b, _ := json.Marshal(req)
err := sr.nls.cmd(string(b))
if err != nil {
return nil, err
}
sr.lk.Lock()
defer sr.lk.Unlock()
sr.stopCh = make(chan bool, 1)
return sr.stopCh, nil
}
func (sr *SpeechRecognition) Shutdown() {
if sr.nls == nil {
return
}
sr.nls.shutdown()
sr.lk.Lock()
defer sr.lk.Unlock()
if sr.startCh != nil {
sr.startCh <- false
close(sr.startCh)
sr.startCh = nil
}
if sr.stopCh != nil {
sr.stopCh <- false
close(sr.stopCh)
sr.stopCh = nil
}
}
func (sr *SpeechRecognition) SendAudioData(data []byte) error {
if sr.nls == nil {
return errors.New("empty nls: using NewSpeechRecognition to create a valid instance")
}
return sr.nls.sendRawData(data)
}