-
Notifications
You must be signed in to change notification settings - Fork 0
/
speak.go
120 lines (103 loc) · 2.55 KB
/
speak.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
package azuretts
import (
"encoding/xml"
"fmt"
)
type Payload interface {
ToXML() ([]byte, error)
}
type Prosody struct {
SpeechText string `xml:",chardata"`
Rate string `xml:"rate,attr"`
Volume string `xml:"volume,attr"`
}
type ExpressAs struct {
// Text string `xml:",chardata"`
Style Style `xml:"style,attr"`
Styledegree string `xml:"styledegree,attr"`
Prosody Prosody `xml:"prosody"`
Role Role `xml:"role,attr,omitempty"`
}
type Voice struct {
// Text string `xml:",chardata"`
Name VoiceName `xml:"name,attr"`
ExpressAs ExpressAs `xml:"mstts:express-as"`
Effect Effect `xml:"effect,omitempty"`
}
// Speak is the root element of the SSML document.
//
// https://learn.microsoft.com/en-us/azure/ai-services/speech-service/speech-synthesis-markup-structure
type Speak struct {
XMLName xml.Name `xml:"speak"`
// Text string `xml:",chardata"`
Version string `xml:"version,attr"`
Xmlns string `xml:"xmlns,attr"`
Mstts string `xml:"xmlns:mstts,attr"`
Lang Language `xml:"xml:lang,attr"`
Voice Voice `xml:"voice"`
}
func (s *Speak) ToXML() ([]byte, error) {
return xml.MarshalIndent(s, "", " ")
}
type SpeakOption func(*Speak)
func WithStyle(style Style) SpeakOption {
return func(s *Speak) {
s.Voice.ExpressAs.Style = style
}
}
func WithVoiceName(name VoiceName) SpeakOption {
return func(s *Speak) {
s.Voice.Name = name
}
}
func WithSpeechText(text string) SpeakOption {
return func(s *Speak) {
s.Voice.ExpressAs.Prosody.SpeechText = text
}
}
func WithLanguage(lang Language) SpeakOption {
return func(s *Speak) {
s.Lang = lang
}
}
func WithRate(rate float64) SpeakOption {
return func(s *Speak) {
s.Voice.ExpressAs.Prosody.Rate = fmt.Sprintf("%.2f", rate)
}
}
func WithVoiceStyledegree(degree float64) SpeakOption {
return func(s *Speak) {
s.Voice.ExpressAs.Styledegree = fmt.Sprintf("%.2f", degree)
}
}
func WithVolume(volume int) SpeakOption {
return func(s *Speak) {
s.Voice.ExpressAs.Prosody.Volume = fmt.Sprintf("%d", volume)
}
}
func NewDefaultSpeak() *Speak {
return &Speak{
Version: "1.0",
Xmlns: "https://www.w3.org/2001/10/synthesis",
Mstts: "https://www.w3.org/2001/mstts",
Voice: Voice{
Name: VoiceNameEnUSAIGenerate1Neural,
ExpressAs: ExpressAs{
Style: "chat",
Styledegree: "1",
Prosody: Prosody{
Rate: "1.0",
Volume: "100",
SpeechText: "",
},
},
},
}
}
func NewSpeak(opts ...SpeakOption) *Speak {
s := NewDefaultSpeak()
for _, opt := range opts {
opt(s)
}
return s
}