-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts.go
217 lines (184 loc) · 5.96 KB
/
tts.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
package speech
import (
"bytes"
"errors"
"fmt"
"io"
"time"
"github.com/Microsoft/cognitive-services-speech-sdk-go/audio"
"github.com/Microsoft/cognitive-services-speech-sdk-go/common"
"github.com/Microsoft/cognitive-services-speech-sdk-go/speech"
"github.com/rs/zerolog/log"
)
func synthesizeStartedHandler(event speech.SpeechSynthesisEventArgs) {
defer event.Close()
fmt.Println("Synthesis started.")
}
func synthesizingHandler(event speech.SpeechSynthesisEventArgs) {
defer event.Close()
fmt.Printf("Synthesizing, audio chunk size %d.\n", len(event.Result.AudioData))
}
func synthesizedHandler(event speech.SpeechSynthesisEventArgs) {
defer event.Close()
fmt.Printf("Synthesized, audio length %d.\n", len(event.Result.AudioData))
}
func cancelledHandler(event speech.SpeechSynthesisEventArgs) {
defer event.Close()
fmt.Println("Received a cancellation.")
}
func (s *Server) Tts(text string, voiceName string) (*bytes.Buffer, error) {
audioBuffer := bytes.Buffer{}
audioConfig, err := audio.NewAudioConfigFromDefaultSpeakerOutput()
if err != nil {
log.Fatal().Msgf("AudioConfig Got an error: %v", err)
return &audioBuffer, err
}
defer audioConfig.Close()
speechConfig, err := speech.NewSpeechConfigFromSubscription(s.SpeechKey, s.SpeechRegion)
if err != nil {
log.Fatal().Msgf("SpeechConfig Got an error: %v", err)
return &audioBuffer, err
}
defer speechConfig.Close()
// "zh-CN-XiaoyouNeural"
err = speechConfig.SetSpeechSynthesisVoiceName(voiceName)
if err != nil {
log.Fatal().Msgf("SetSpeechSynthesisVoiceName Got an error: %v", err)
return &audioBuffer, err
}
err = speechConfig.SetSpeechSynthesisOutputFormat(common.Riff16Khz16BitMonoPcm)
if err != nil {
log.Fatal().Msgf("SetSpeechSynthesisOutputFormat Got an error: %v", err)
return &audioBuffer, err
}
speechSynthesizer, err := speech.NewSpeechSynthesizerFromConfig(speechConfig, audioConfig)
if err != nil {
log.Fatal().Msgf("SpeechSynthesizer Got an error: %v", err)
return &audioBuffer, err
}
defer speechSynthesizer.Close()
speechSynthesizer.SynthesisStarted(synthesizeStartedHandler)
speechSynthesizer.Synthesizing(synthesizingHandler)
speechSynthesizer.SynthesisCompleted(synthesizedHandler)
speechSynthesizer.SynthesisCanceled(cancelledHandler)
task := speechSynthesizer.SpeakTextAsync(text)
var outcome speech.SpeechSynthesisOutcome
select {
case outcome = <-task:
case <-time.After(60 * time.Second):
log.Error().Msgf("tts timeout")
return &audioBuffer, err
}
defer outcome.Close()
if outcome.Error != nil {
log.Err(outcome.Error).Msgf("Stream tts got an error!")
return &audioBuffer, err
}
// in most case we want to streaming receive the audio to lower the latency,
// we can use AudioDataStream to do so.
stream, err := speech.NewAudioDataStreamFromSpeechSynthesisResult(outcome.Result)
defer stream.Close()
if err != nil {
log.Err(err).Msgf("NewAudioDataStreamFromSpeechSynthesisResult stream tts got an error!")
return &audioBuffer, err
}
// var allAudio []byte
audioChunk := make([]byte, 2048)
for {
n, err := stream.Read(audioChunk)
if err == io.EOF {
break
}
audioBuffer.Write(audioChunk[:n])
}
return &audioBuffer, nil
}
type BinaryMessage struct {
Err error
Data []byte
}
type TtsRequest struct {
Start func() <-chan *BinaryMessage
Close func()
}
type TtsService func() *TtsRequest
func (s *Server) TtsStream(text string, voiceName string) (TtsService, error) {
audioConfig, err := audio.NewAudioConfigFromDefaultSpeakerOutput()
if err != nil {
log.Fatal().Msgf("AudioConfig Got an error: %v", err)
return nil, err
}
defer audioConfig.Close()
speechConfig, err := speech.NewSpeechConfigFromSubscription(s.SpeechKey, s.SpeechRegion)
if err != nil {
log.Fatal().Msgf("SpeechConfig Got an error: %v", err)
return nil, err
}
defer speechConfig.Close()
// "zh-CN-XiaoyouNeural"
err = speechConfig.SetSpeechSynthesisVoiceName(voiceName)
if err != nil {
log.Fatal().Msgf("SetSpeechSynthesisVoiceName Got an error: %v", err)
return nil, err
}
err = speechConfig.SetSpeechSynthesisOutputFormat(common.Riff16Khz16BitMonoPcm)
if err != nil {
log.Fatal().Msgf("SetSpeechSynthesisOutputFormat Got an error: %v", err)
return nil, err
}
speechSynthesizer, err := speech.NewSpeechSynthesizerFromConfig(speechConfig, audioConfig)
if err != nil {
log.Fatal().Msgf("SpeechSynthesizer Got an error: %v", err)
return nil, err
}
defer speechSynthesizer.Close()
speechSynthesizer.SynthesisStarted(synthesizeStartedHandler)
speechSynthesizer.Synthesizing(synthesizingHandler)
speechSynthesizer.SynthesisCompleted(synthesizedHandler)
speechSynthesizer.SynthesisCanceled(cancelledHandler)
if len(text) == 0 {
return nil, errors.New("text is null")
}
// StartSpeakingTextAsync sends the result to channel when the synthesis starts.
task := speechSynthesizer.StartSpeakingTextAsync(text)
var outcome speech.SpeechSynthesisOutcome
return func() *TtsRequest {
start := func() <-chan *BinaryMessage {
ch := make(chan *BinaryMessage)
go func() {
select {
case outcome = <-task:
case <-time.After(60 * time.Second):
log.Error().Msgf("tts timeout")
ch <- &BinaryMessage{Err: errors.New("tts timeout")}
return
}
defer outcome.Close()
if outcome.Error != nil {
log.Err(outcome.Error).Msgf("Stream tts got an error!")
ch <- &BinaryMessage{Err: outcome.Error}
return
}
// todo: 可以使用尝试使用synthesizingHandler来处理流
stream, err := speech.NewAudioDataStreamFromSpeechSynthesisResult(outcome.Result)
defer stream.Close()
if err != nil {
log.Err(err).Msgf("NewAudioDataStreamFromSpeechSynthesisResult stream tts got an error!")
ch <- &BinaryMessage{Err: err}
return
}
audioChunk := make([]byte, 2048)
for {
n, err := stream.Read(audioChunk)
if err == io.EOF {
ch <- &BinaryMessage{Err: err}
break
}
ch <- &BinaryMessage{Data: audioChunk[:n]}
}
}()
return ch
}
return &TtsRequest{Start: start}
}, nil
}