-
Notifications
You must be signed in to change notification settings - Fork 0
/
communicate.go
511 lines (454 loc) · 13.6 KB
/
communicate.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package communicate
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/lib-x/edgetts/internal/businessConsts"
"github.com/lib-x/edgetts/internal/communicateOption"
"github.com/lib-x/edgetts/internal/validate"
"golang.org/x/net/proxy"
"html"
"io"
"net"
"net/http"
"net/url"
"runtime/debug"
"strings"
"sync"
"time"
"unicode"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
const (
ssmlHeaderTemplate = "X-RequestId:%s\r\nContent-Type:application/ssml+xml\r\nX-Timestamp:%sZ\r\nPath:ssml\r\n\r\n"
ssmlTemplate = "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='%s'><prosody pitch='+0Hz' rate='%s' volume='%s'>%s</prosody></voice></speak>"
)
var (
headerOnce = &sync.Once{}
escapeReplacer = strings.NewReplacer(">", ">", "<", "<")
communicateHeader http.Header
)
func init() {
headerOnce.Do(func() {
communicateHeader = makeHeaders()
})
}
type Communicate struct {
text string
voice string
voiceLanguageRegion string
rate string
volume string
httpProxy string
socket5Proxy string
socket5ProxyUser string
socket5ProxyPass string
op chan map[string]interface{}
AudioDataIndex int
}
type textEntry struct {
Text string `json:"text"`
Length int64 `json:"Length"`
BoundaryType string `json:"BoundaryType"`
}
type dataEntry struct {
Offset int `json:"Offset"`
Duration int `json:"Duration"`
Text textEntry `json:"text"`
}
type metaDataEntry struct {
Type string `json:"Type"`
Data dataEntry `json:"Data"`
}
type audioData struct {
Data []byte
Index int
}
type unknownResponse struct {
Message string
}
type unexpectedResponse struct {
Message string
}
type noAudioReceived struct {
Message string
}
type webSocketError struct {
Message string
}
func NewCommunicate(text string, opt *communicateOption.CommunicateOption) (*Communicate, error) {
if opt == nil {
opt = &communicateOption.CommunicateOption{}
}
opt.CheckAndApplyDefaultOption()
if err := validate.WithCommunicateOption(opt); err != nil {
return nil, err
}
return &Communicate{
text: text,
voice: opt.Voice,
voiceLanguageRegion: opt.VoiceLangRegion,
rate: opt.Rate,
volume: opt.Volume,
httpProxy: opt.HttpProxy,
socket5Proxy: opt.Socket5Proxy,
socket5ProxyUser: opt.Socket5ProxyUser,
socket5ProxyPass: opt.Socket5ProxyPass,
}, nil
}
// WriteStreamTo write audio stream to io.WriteCloser
func (c *Communicate) WriteStreamTo(rc io.Writer) error {
op, err := c.stream()
if err != nil {
return err
}
audioBinaryData := make([][][]byte, c.AudioDataIndex)
for i := range op {
if _, ok := i["end"]; ok {
if len(audioBinaryData) == c.AudioDataIndex {
break
}
}
if t, ok := i["type"]; ok && t == "audio" {
data := i["data"].(audioData)
audioBinaryData[data.Index] = append(audioBinaryData[data.Index], data.Data)
}
if e, ok := i["error"]; ok {
fmt.Printf("has error err: %v\n", e)
}
}
for _, dataSlice := range audioBinaryData {
for _, data := range dataSlice {
rc.Write(data)
}
}
return nil
}
func (c *Communicate) CloseOutput() {
close(c.op)
}
func makeHeaders() http.Header {
header := make(http.Header)
header.Set("Pragma", "no-cache")
header.Set("Cache-Control", "no-cache")
header.Set("Origin", "chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold")
header.Set("Accept-Encoding", "gzip, deflate, br")
header.Set("Accept-Language", "en-US,en;q=0.9")
header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41")
return header
}
func (c *Communicate) stream() (<-chan map[string]interface{}, error) {
texts := splitTextByByteLength(
escape(removeIncompatibleCharacters(c.text)),
calculateMaxMessageSize(c.voice, c.rate, c.volume),
)
c.AudioDataIndex = len(texts)
finalUtterance := make(map[int]int)
prevIdx := -1
shiftTime := -1
output := make(chan map[string]interface{})
for idx, text := range texts {
wsURL := businessConsts.EdgeWssEndpoint + "&ConnectionId=" + generateConnectID()
dialer := websocket.Dialer{}
setupWebSocketProxy(&dialer, c)
conn, _, err := dialer.Dial(wsURL, communicateHeader)
if err != nil {
return nil, err
}
// Each message needs to have the proper date.
date := currentTimeInMST()
// Prepare the request to be sent to the service.
//
// Note sentenceBoundaryEnabled and wordBoundaryEnabled are actually supposed
// to be booleans, but Edge Browser seems to send them as strings.
//
// This is a bug in Edge as Azure Cognitive Services actually sends them as
// bool and not string. For now I will send them as bool unless it causes
// any problems.
//
// Also pay close attention to double { } in request (escape for f-string).
err = conn.WriteMessage(websocket.TextMessage, []byte(
"X-Timestamp:"+date+"\r\n"+
"Content-Type:application/json; charset=utf-8\r\n"+
"Path:speech.config\r\n\r\n"+
`{"context":{"synthesis":{"audio":{"metadataoptions":{"sentenceBoundaryEnabled":false,"wordBoundaryEnabled":true},"outputFormat":"audio-24khz-48kbitrate-mono-mp3"}}}}`+"\r\n",
))
if err != nil {
conn.Close()
return nil, err
}
err = conn.WriteMessage(websocket.TextMessage, []byte(
ssmlHeadersAppendExtraData(
generateConnectID(),
date,
makeSsml(string(text), c.voice, c.rate, c.volume),
),
))
if err != nil {
conn.Close()
return nil, err
}
go func(conn *websocket.Conn, idx int) {
// download indicates whether we should be expecting audio data,
// this is so what we avoid getting binary data from the websocket
// and falsely thinking it's audio data.
downloadAudio := false
// audioWasReceived indicates whether we have received audio data
// from the websocket. This is so we can raise an exception if we
// don't receive any audio data.
audioWasReceived := false
defer conn.Close()
defer func() {
if err := recover(); err != nil {
fmt.Printf("communicate.Stream recovered from panic: %v stack: %s", err, string(debug.Stack()))
}
}()
for {
msgType, message, err := conn.ReadMessage()
if err != nil {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
// WebSocket error
output <- map[string]interface{}{
"error": webSocketError{Message: err.Error()},
}
}
break
}
switch msgType {
case websocket.TextMessage:
parameters, data := processWebsocketTextMessage(message)
path := parameters["Path"]
switch path {
case "turn.start":
downloadAudio = true
case "turn.end":
output <- map[string]interface{}{
"end": "",
}
downloadAudio = false
break // End of audio data
case "audio.metadata":
metadata, err := processMetadata(data)
if err != nil {
output <- map[string]interface{}{
"error": unknownResponse{Message: err.Error()},
}
break
}
for _, metaObj := range metadata {
metaType := metaObj.Type
if idx != prevIdx {
shiftTime = sumWithMap(idx, finalUtterance)
prevIdx = idx
}
switch metaType {
case "WordBoundary":
finalUtterance[idx] = metaObj.Data.Offset + metaObj.Data.Duration + 8_750_000
output <- map[string]interface{}{
"type": metaType,
"offset": metaObj.Data.Offset + shiftTime,
"duration": metaObj.Data.Duration,
"text": metaObj.Data.Text,
}
case "SessionEnd":
// do nothing
default:
output <- map[string]interface{}{
"error": unknownResponse{Message: "Unknown metadata type: " + metaType},
}
break
}
}
case "response":
// do nothing
default:
output <- map[string]interface{}{
"error": unknownResponse{Message: "The response from the service is not recognized.\n" + string(message)},
}
break
}
case websocket.BinaryMessage:
if !downloadAudio {
output <- map[string]interface{}{
"error": unknownResponse{"We received a binary message, but we are not expecting one."},
}
}
if len(message) < 2 {
output <- map[string]interface{}{
"error": unknownResponse{"We received a binary message, but it is missing the header length."},
}
}
headerLength := int(binary.BigEndian.Uint16(message[:2]))
if len(message) < headerLength+2 {
output <- map[string]interface{}{
"error": unknownResponse{"We received a binary message, but it is missing the audio data."},
}
}
audioBinaryData := message[headerLength+2:]
output <- map[string]interface{}{
"type": "audio",
"data": audioData{
Data: audioBinaryData,
Index: idx,
},
}
audioWasReceived = true
default:
if message != nil {
output <- map[string]interface{}{
"error": webSocketError{
Message: string(message),
},
}
} else {
output <- map[string]interface{}{
"error": webSocketError{
Message: "Unknown error",
},
}
}
}
}
if !audioWasReceived {
output <- map[string]interface{}{
"error": noAudioReceived{Message: "No audio was received. Please verify that your parameters are correct."},
}
}
}(conn, idx)
}
c.op = output
return output, nil
}
func sumWithMap(idx int, m map[int]int) int {
sumResult := 0
for i := 0; i < idx; i++ {
sumResult += m[i]
}
return sumResult
}
func setupWebSocketProxy(dialer *websocket.Dialer, c *Communicate) {
if c.httpProxy != "" {
proxyUrl, _ := url.Parse(c.httpProxy)
dialer.Proxy = http.ProxyURL(proxyUrl)
}
if c.socket5Proxy != "" {
auth := &proxy.Auth{User: c.socket5ProxyUser, Password: c.socket5ProxyPass}
socket5ProxyDialer, _ := proxy.SOCKS5("tcp", c.socket5Proxy, auth, proxy.Direct)
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
return socket5ProxyDialer.Dial(network, address)
}
dialer.NetDialContext = dialContext
}
}
func processMetadata(data []byte) ([]metaDataEntry, error) {
var metadata struct {
Metadata []metaDataEntry `json:"Metadata"`
}
err := json.Unmarshal(data, &metadata)
if err != nil {
return nil, fmt.Errorf("err=%s, data=%s", err.Error(), string(data))
}
return metadata.Metadata, nil
}
func processWebsocketTextMessage(data []byte) (map[string]string, []byte) {
headers := make(map[string]string)
headerEndIndex := bytes.Index(data, []byte("\r\n\r\n"))
headerLines := bytes.Split(data[:headerEndIndex], []byte("\r\n"))
for _, line := range headerLines {
header := bytes.SplitN(line, []byte(":"), 2)
if len(header) == 2 {
headers[string(bytes.TrimSpace(header[0]))] = string(bytes.TrimSpace(header[1]))
}
}
return headers, data[headerEndIndex+4:]
}
func removeIncompatibleCharacters(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsControl(r) && r != '\t' && r != '\n' && r != '\r' {
return ' '
}
return r
}, str)
}
func generateConnectID() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")
}
// splitTextByByteLength splits the input text into chunks of a specified byte length.
// The function ensures that the text is not split in the middle of a word. If a word exceeds the specified byte length,
// the word is placed in the next chunk. The function returns a slice of byte slices, each representing a chunk of the original text.
//
// Parameters:
// text: The input text to be split.
// byteLength: The maximum byte length for each chunk of text.
//
// Returns:
// A slice of byte slices, each representing a chunk of the original text.
func splitTextByByteLength(text string, byteLength int) [][]byte {
var result [][]byte
textBytes := []byte(text)
if byteLength > 0 {
for len(textBytes) > byteLength {
splitAt := bytes.LastIndexByte(textBytes[:byteLength], ' ')
if splitAt == -1 || splitAt == 0 {
splitAt = byteLength
} else {
splitAt++
}
trimmedText := bytes.TrimSpace(textBytes[:splitAt])
if len(trimmedText) > 0 {
result = append(result, trimmedText)
}
textBytes = textBytes[splitAt:]
}
}
trimmedText := bytes.TrimSpace(textBytes)
if len(trimmedText) > 0 {
result = append(result, trimmedText)
}
return result
}
func makeSsml(text string, voice string, rate string, volume string) string {
ssml := fmt.Sprintf(ssmlTemplate,
voice,
rate,
volume,
text)
return ssml
}
func currentTimeInMST() string {
// Use time.FixedZone to represent a fixed timezone offset of 0 (UTC)
zone := time.FixedZone("UTC", 0)
now := time.Now().In(zone)
return now.Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
}
func ssmlHeadersAppendExtraData(requestID string, timestamp string, ssml string) string {
headers := fmt.Sprintf(ssmlHeaderTemplate,
requestID,
timestamp,
)
return headers + ssml
}
func calculateMaxMessageSize(voice string, rate string, volume string) int {
websocketMaxSize := 1 << 16
overheadPerMessage := len(ssmlHeadersAppendExtraData(generateConnectID(), currentTimeInMST(), makeSsml("", voice, rate, volume))) + 50
return websocketMaxSize - overheadPerMessage
}
func escape(data string) string {
// Must do ampersand first
entities := make(map[string]string)
data = html.EscapeString(data)
data = escapeReplacer.Replace(data)
if entities != nil {
data = replaceWithDict(data, entities)
}
return data
}
func replaceWithDict(data string, entities map[string]string) string {
for key, value := range entities {
data = strings.ReplaceAll(data, key, value)
}
return data
}