-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
215 lines (186 loc) · 5.28 KB
/
types.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
package cosyvoice
import (
"encoding/json"
"fmt"
)
type Header struct {
Action Action `json:"action"`
TaskID string `json:"task_id"`
Streaming string `json:"streaming"`
Event string `json:"event"`
Attributes map[string]interface{} `json:"attributes"`
Error Error `json:"error,omitempty"`
}
type Error struct {
Code string `json:"error_code,omitempty"`
Message string `json:"error_message,omitempty"`
}
func (e Error) Error() string {
return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
}
type Format string
const (
FormatPCM Format = "pcm"
FormatWAV Format = "wav"
FormatMP3 Format = "mp3"
)
type SampleRate int
const (
SampleRate8000 SampleRate = 8000
SampleRate16000 SampleRate = 16000
SampleRate22050 SampleRate = 22050
SampleRate24000 SampleRate = 24000
SampleRate44100 SampleRate = 44100
SampleRate48000 SampleRate = 48000
)
const (
TextTypePlainText string = "PlainText"
)
type Model string
const (
ModelCosyvoiceV1 Model = "cosyvoice-v1"
)
type Payload struct {
TaskGroup string `json:"task_group"`
Task string `json:"task"`
Function string `json:"function"`
Model Model `json:"model"`
Parameters Params `json:"parameters"`
Resources []Resource `json:"resources"`
Input Input `json:"input"`
}
type Params struct {
TextType string `json:"text_type"` // PlainText
Voice Voice `json:"voice"` // longwan, longcheng, longhua, etc.
Format Format `json:"format"` // pcm, wav, mp3
SampleRate SampleRate `json:"sample_rate"` // 8000, 16000, 22050, 24000, 44100, 48000
Volume int `json:"volume,omitempty"` // range: 0~100, default: 50
Rate int `json:"rate,omitempty"` // range: 0.5~2, default: 1.0
Pitch int `json:"pitch,omitempty"` // range: 0.5~2, default: 1.0
}
type Resource struct {
ResourceID string `json:"resource_id"`
ResourceType string `json:"resource_type"`
}
type Input struct {
Text string `json:"text"`
}
type Event struct {
Header Header `json:"header"`
Payload Payload `json:"payload"`
}
type SynthesizerConfig struct {
Model Model `json:"model"` // cosyvoice-v1
Voice Voice `json:"voice"` // longwan, longcheng, longhua, etc.
Format Format `json:"format"` // pcm, wav, mp3
SampleRate SampleRate `json:"sample_rate"` // 8000, 16000, 22050, 24000, 44100, 48000
Volume int `json:"volume,omitempty"` // range: 0~100, default: 50
Rate int `json:"rate,omitempty"` // range: 0.5~2, default: 1.0
Pitch int `json:"pitch,omitempty"` // range: 0.5~2, default: 1.0
}
func DefaultSynthesizerConfig() SynthesizerConfig {
return SynthesizerConfig{
Model: "cosyvoice-v1",
Voice: Longxiaochun,
Format: "mp3",
SampleRate: 16000,
}
}
type Voice string
const (
Longwan Voice = "longwan"
Longcheng Voice = "longcheng"
Longhua Voice = "longhua"
Longxiaochun Voice = "longxiaochun"
Longxiaoxia Voice = "longxiaoxia"
Longxiaocheng Voice = "longxiaocheng"
Longxiaobai Voice = "longxiaobai"
Longlaotie Voice = "longlaotie"
Longshu Voice = "longshu"
Longjing Voice = "longjing"
Longmiao Voice = "longmiao"
Longyue Voice = "longyue"
Longyuan Voice = "longyuan"
Longfei Voice = "longfei"
Longjielidou Voice = "longjielidou"
Longshuo Voice = "longshuo"
Longtong Voice = "longtong"
Longxiang Voice = "longxiang"
Loongstella Voice = "loongstella"
Loongbella Voice = "loongbella"
)
type Action string
const (
ActionRunTask Action = "run-task"
ActionContinueTask Action = "continue-task"
ActionFinishTask Action = "finish-task"
)
const (
StreamingDuplex string = "duplex"
)
const (
TaskGroupAudio string = "audio"
)
const (
TaskTTS string = "tts"
)
const (
FunctionSpeechSynthesizer string = "SpeechSynthesizer"
)
func generateRunTaskCmd(taskID string, voiceConfig SynthesizerConfig) (string, error) {
cmd := Event{
Header: Header{
Action: ActionRunTask,
TaskID: taskID,
Streaming: StreamingDuplex,
},
Payload: Payload{
TaskGroup: TaskGroupAudio,
Task: TaskTTS,
Function: FunctionSpeechSynthesizer,
Model: voiceConfig.Model,
Parameters: Params{
TextType: TextTypePlainText,
Voice: voiceConfig.Voice,
Format: voiceConfig.Format,
SampleRate: voiceConfig.SampleRate,
Volume: voiceConfig.Volume,
Rate: voiceConfig.Rate,
Pitch: voiceConfig.Pitch,
},
Input: Input{},
},
}
data, err := json.Marshal(cmd)
return string(data), err
}
func generateContinueTaskCmd(taskID string, text string) (string, error) {
cmd := Event{
Header: Header{
Action: ActionContinueTask,
TaskID: taskID,
Streaming: StreamingDuplex,
},
Payload: Payload{
Input: Input{
Text: text,
},
},
}
data, err := json.Marshal(cmd)
return string(data), err
}
func generateFinishTaskCmd(taskID string) (string, error) {
cmd := Event{
Header: Header{
Action: ActionFinishTask,
TaskID: taskID,
Streaming: StreamingDuplex,
},
Payload: Payload{
Input: Input{},
},
}
data, err := json.Marshal(cmd)
return string(data), err
}